14. Longest Common Prefix
Easy
Input: strs = ["flower","flow","flight"]
Output: "fl"Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
prefix = ''
for index in range(len(strs[0])):
char = strs[0][index]
for string in strs[1:]:
if index >= len(string):
return prefix
elif string[:index+1] != strs[0][:index+1]:
return prefix
prefix += char
return prefixLast updated