Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
Approach1: Vertical Scanning
This approach consists of iterating over the characters of an arbitrarily chosen String from the array (in this example, we use the string at index 0
) and verify by looping on each of the Strings of the array if they contain that same character at the position specified by the initially chosen String (Outer Loop).
Iterate over first String of given array (in lieu of a traditionnal loop or a for-each loop, we use enumerate to simultaneously get the position of the pointer as well as the character it is pointing to.)
We loop over the Strings of the array and check whether they have the same character at position
i
thenstrs[0]
.We stop the loop for the below conditions:
If
i
equals or surpasses the length of a given word meaning that there cannot be a longer common prefix, we then return the substring.If the chracters are different at position
i
, we return thesubstring[0:i]
indicating that it is the longest prefix shared by all the strings constituting the array.
Time: O(mn) m = Length of longest String n = length of array strs. Space: O(1)
Java Version of Vertical Scanning
Approach2: Horizontal Scanning
The code for this approach as well as it's underlying concept is relatively easier to grasp than the above.
We again select the first String (strs[0]) and reference it to a string holder called
pre.
We then loop over each subsequent Strings in the array and check whether or not they start with
pre.
If the String does not start with
pre,
we subseqently reduce it by one index from the right by getting it's substring. We use the while loop as we want to ensure that for each iteration,pre
is being reduced until it is a prefix.
O(n
m
(m + m))
, which is O(n
m
2m)
, which is O(n * m^2)
Because for n
strings it does m
times startswith (which is O(m))
and m times substring (which is O(m)).
Java Version of Vertical Scanning
Last updated
Was this helpful?