14. Longest Common Prefix - cocoder39/coco39_LC GitHub Wiki
Notes 2022
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ''
shortest = min(strs, key=len)
for i, ch in enumerate(shortest):
for other in strs:
if ch != other[i]:
return shortest[:i]
return shortest
=========================================================
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) {
return "";
}
string res;
for(int i = 0; i < strs[0].length(); i++){
for(int j = 1; j < strs.size(); j++){
if(i >= strs[j].length() || strs[j][i] != strs[0][i]){
return res;
}
}
res.push_back(strs[0][i]);
}
return res;
}