Longest Common Prefix
Input: ["flower","flow","flight"]
Output: "fl"Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ''
strs.sort(key=len)
cur = strs[0]
for i,s in enumerate(cur):
for others in strs[1:]:
if others[i]!=s:
return cur[:i]
return cur
Last updated