EasyStrings
Longest Common Prefix
googleamazonappleadobemicrosoft
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Examples
Example 1
Input
strs = ["flower","flow","flight"]
Output
"fl"
All three strings start with "fl". The third string diverges at index 2 ('i' vs 'o'), so the longest common prefix is "fl".
Example 2
Input
strs = ["dog","racecar","car"]
Output
""
There is no character that all three strings share at index 0 ('d' vs 'r' vs 'c'), so the common prefix is empty.
Constraints
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] consists of only lowercase English letters.
Optimal complexity
Time
O(n * m)
Space
O(1)
One problem, two ways to prep
Choose between solo practice and interview simulation
Practice Mode keeps things simple with code + tests. AI Interview Mode adds voice, pressure, and a post-round score summary.