Skip to main content

14. Longest Common Prefix

https://leetcode.com/problems/longest-common-prefix/

Python

class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""

limit = min([len(s) for s in strs])

prefix = []
for i in range(limit):
cur = strs[0][i]
if all([s[i]==cur for s in strs[1:]]):
prefix.append(cur)
continue
break

return ''.join(prefix)