LeetCode #14: Longest Common Prefix
β’ 242 words β’ 2 min β’ updated
LeetCode #14: Longest Common Prefix:
Modern:
python
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
min_len = min(len(s) for s in strs)
for i in range(min_len):
c = strs[0][i]
for s in strs[1:]:
if c != s[i]:
return strs[0][:i]
return strs[0][:min_len]Elegant:
python
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
prefix = ""
for i in range(min(len(s) for s in strs)):
# ('f', 'f', 'f') -> (True, True, True)
if all(strs[0][i] == s[i] for s in strs[1:]):
prefix += strs[0][i]
else:
break
return prefixBoth for s in strs[1:] and for s in strs work, the latter is redundant.
Previously:
python
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
ans = ""
min_length = min([len(str) for str in strs])
for i in range(min_length):
c = strs[0][i]
the_end = False
for str in strs[1:]:
if str[i] != c:
the_end = True
break
if the_end:
break
ans += c
return ansWhen you do min(), max(), sum(), etc: there’s no need to create an
intermediate list. In other words, this simplification is more concise and
elegant:
python
min_length = min([len(str) for str in strs]) # ->
min_length = min(len(str) for str in strs) # ->the_end is a workaround to break from a nested loop, as there is no goto in
Python. And it is considered
harmful anyway.
Another way is to throw an exception, but that’s more cumbersome and error-prone
in the context of a coding interview.