easyStringsPattern: Two Pointers
LongestCommonPrefixFinder Solution
Problem Statement
You are given two strings, find the length of the longest common prefix.
Examples
Example 1:
Input:['abc', 'abcde']
Output:3
Explanation: The longest common prefix is 'abc'.
Example 2:
Input:["","hello"]
Output:0
Explanation: The empty string is a prefix of every string.
Example 3:
Input:["aaa","aaa"]
Output:3
Explanation: The entire string is a common prefix.
Constraints
- The input strings are not allowed to be null.
- The input strings contain only lowercase English letters.
- The strings can be of different length.
- You are not allowed to use extra space.
- The time complexity should be O(m), where m is the length of the shorter string.
Time: O(m + n) Space: O(1)
Use two pointers technique to traverse both strings simultaneously. Return the length of the common prefix when a mismatch is found.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
