Longest Common Prefix

Write a function to find the longest common prefix string among an array of strings strs.

If there is no common prefix, return an empty string "".

Example 1
Inputstrs = ["flower","flow","flight"]
Output"fl"
The strings share the prefix "fl", and the next characters differ.
Example 2
Inputstrs = ["dog","racecar","car"]
Output""
The first characters are different, so there is no common prefix.

Constraints

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters if it is non-empty

Asked at 57 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate