Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
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 = strs[0]
for i in range(1, len(strs)):
minLen = min(len(prefix),len(strs[i]))
temp = ""
for j in range(minLen):
if prefix[j] == strs[i][j]:
temp += prefix[j]
else:
break
prefix = temp
return prefix
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
ans=""
strs=sorted(strs)
first=strs[0]
last=strs[-1]
for i in range(min(len(first),len(last))):
if(first[i]!=last[i]):
return ans
ans+=first[i]
return ans