Talentd
FleetCode

14. Longest Common Prefix

Easy48.6% acceptance60 companies

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

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters.
6:31
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def longestCommonPrefix(self, strs: List[str]) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
["flower","flow","flight"]
Input
["dog","racecar","car"]
Your code is saved on this device.Solution