Last Updated: March 20, 2026
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.
One important detail: any string in the array could be empty, and an empty string has no characters to share. So if even one string is "", the answer is immediately "". Similarly, the LCP can never be longer than the shortest string in the array, because a prefix of a shorter string can't be longer than the string itself.
The core challenge is figuring out an efficient way to compare characters across all strings simultaneously.
1 <= strs.length <= 200 → The array has at most 200 strings. Even O(n^2) approaches on the number of strings are fine.0 <= strs[i].length <= 200 → Each string is at most 200 characters. A string can be empty.The most natural way to find a common prefix is to check character by character. Look at position 0 of every string. Are they all the same? If yes, move to position 1. Keep going until you find a position where the characters differ, or you run out of characters in some string.
Think of the strings stacked on top of each other like rows in a grid. You're scanning down each column. The moment a column isn't uniform, you stop.
""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 farThe vertical scanning approach is already optimal in the worst case since we have to look at every character at least once. But there are other interesting strategies. The sorting approach, for instance, avoids comparing all strings against each other by leveraging a neat property of lexicographic order.
Here's a clever observation: if you sort the array of strings lexicographically, the first and last strings in the sorted order are the most different from each other. Any prefix that's common to both of these "extreme" strings must also be common to every string in between.
Why? Because sorting arranges 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 must also start with "fl". So instead of checking all n strings, we just sort and compare two.
""The sorting approach trades optimal time complexity for elegance. Divide and conquer offers yet another structural perspective, splitting the problem into halves and combining results.
Divide and conquer gives us a different angle on this problem. The key observation is: the longest common prefix of an array of strings is the same as the longest common prefix of (the LCP of the left half) and (the LCP of the right half).
In other words, LCP(S1, S2, ..., Sn) = LCP(LCP(S1, ..., Smid), LCP(Smid+1, ..., Sn)).
We keep splitting until we reach individual strings (which are their own LCP), then merge results back up by finding the common prefix of two strings at each level. This is the same pattern as merge sort: split the problem, solve each half, combine the results.
""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