LongestCommonPrefix
Problem
Solution
Iterate through each character in the shortest string and check if it is in all
other strings in strs
.
Code
https://github.com/GeorgeRPu/tech-interview-prep/blob/main/solutions/LongestCommonPrefix.py
from typing import List
def longestCommonPrefix(strs: List[str]) -> str:
"""Find longest common prefix of all strings in ``strs``.
"""
shortest = min(strs, key=len)
for i, char in enumerate(shortest):
if any(other[i] != char for other in strs):
return shortest[:i]
return shortest
Test
>>> from LongestCommonPrefix import longestCommonPrefix
>>> longestCommonPrefix(['flower', 'flow', 'flight'])
'fl'
>>> longestCommonPrefix(['dog', 'racecar', 'car'])
''
Functions
|
Find longest common prefix of all strings in |