s1 = strs[0] result = "" endFlag = False for inner inrange(len(s1) + 1): sub = s1[0:inner] for out inrange(1, len(strs)): if strs[out].startswith(sub): iflen(result) < len(sub) and out == len(strs)-1: result = sub else: endFlag = True break if endFlag: break return result
1 2 3 4 5 6 7 8 9 10 11
classSolution: deflongestCommonPrefix(self, v: List[str]) -> str: ans="" v=sorted(v) first=v[0] last=v[-1] for i inrange(min(len(first),len(last))): if(first[i]!=last[i]): return ans ans+=first[i] return ans