AlgoMaster Logo

Longest Common Prefix

Last Updated: March 20, 2026

easy
4 min read

Understanding the Problem

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.

Key Constraints:

  • 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.
  • Only lowercase English letters → No need to handle unicode or special characters.

Approach 1: Vertical Scanning

Intuition

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.

Algorithm

  1. If the array is empty, return ""
  2. For each character position i starting from 0:
    • Get the character at position i from the first string
    • For every other string in the array:
      • If i equals the length of that string (we've run past it), return the prefix so far
      • If the character at position i doesn't match, return the prefix so far
  3. If we get through all characters of the first string without stopping, the entire first string is the common prefix

Example Walkthrough

1Check column 0: flower[0]='f', flow[0]='f', flight[0]='f' → All match
0
i=0
f
1
l
2
o
3
w
4
e
5
r
1/4

Code

The 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.

Approach 2: Sorting

Intuition

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.

Algorithm

  1. If the array is empty, return ""
  2. Sort the array of strings lexicographically
  3. Compare the first and last strings character by character
  4. The common prefix of these two strings is the answer

Example Walkthrough

1Input: ["flower", "flow", "flight"]. After sorting: ["flight", "flow", "flower"]
0
first
flight
1
flow
2
last
flower
1/5

Code

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.

Approach 3: Divide and Conquer

Intuition

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.

Algorithm

  1. If the array is empty, return ""
  2. Call a recursive function findLCP(strs, left, right):
    • Base case: If left == right, return strs[left] (a single string is its own prefix)
    • Divide: Find mid = (left + right) / 2
    • Conquer: Recursively find lcpLeft = findLCP(strs, left, mid) and lcpRight = findLCP(strs, mid + 1, right)
    • Combine: Return the common prefix of lcpLeft and lcpRight

Example Walkthrough

1Split ["flower","flow","flight","fly"] into left=["flower","flow"] and right=["flight","fly"]
0
flower
1
flow
2
flight
3
fly
1/4

Code