A prefix of a string is any leading substring. For example, the prefixes of "flower" are "", "f", "fl", "flo", "flow", "flowe", and "flower". The longest common prefix (LCP) is the longest string that is a prefix of every string in the array.
Two properties bound the answer. Any string in the array can be empty, and an empty string shares no characters, so if even one string is "", the answer is "". And the LCP can never be longer than the shortest string in the array, since a prefix of a shorter string cannot exceed that string's length.
The challenge is comparing characters across all strings efficiently.
1 <= strs.length <= 200 → The array has at most 200 strings, so the total character count is small. Time complexity is not the binding concern here; any reasonable scan passes.0 <= strs[i].length <= 200 → A string can be empty, which forces an empty answer. Every approach must handle this case.Check the common prefix one character position at a time. Look at position 0 of every string. If they all match, move to position 1, and continue until a position where the characters differ or some string runs out of characters.
With the strings stacked as rows in a grid, this scans down each column. The prefix ends at the first column that is not uniform, or when the shortest string ends.
""i starting from 0:i from the first stringi equals the length of that string (we've run past it), return the prefix so fari doesn't match, return the prefix so farVertical scanning is already optimal in the worst case, since every character of the prefix has to be confirmed against every string at least once. The next approach uses a property of lexicographic order to avoid touching most strings at all.
If you sort the array of strings lexicographically, the first and last strings in sorted order are the two that differ earliest. Any prefix common to both of these extremes must also be common to every string between them.
This holds because sorting orders strings by their characters from left to right. If the first sorted string starts with "fl" and the last sorted string also starts with "fl", then every string between them starts with "fl" too, otherwise it would have sorted before the first or after the last. So instead of comparing all n strings, sort once and compare two.
""Sorting reduces the work to two strings but pays the O(n m log n) sort cost. The next approach splits the array into halves and combines partial results, which is useful when the strings are spread across machines or computed in parallel.
The LCP of an array equals the LCP of two values: the LCP of the left half and the LCP of the right half. Formally, LCP(S1, ..., Sn) = LCP(LCP(S1, ..., Smid), LCP(Smid+1, ..., Sn)). This is associative because a prefix shared by every string in both halves is a prefix shared by every string overall.
Split the array until each piece is a single string (which is its own LCP), then combine results back up by finding the common prefix of two strings at each level. The structure mirrors merge sort: split, solve each half, combine.
""findLCP(strs, left, right):left == right, return strs[left] (a single string is its own prefix)mid = (left + right) / 2lcpLeft = findLCP(strs, left, mid) and lcpRight = findLCP(strs, mid + 1, right)lcpLeft and lcpRight