HTMLify

LeetCode - Longest Common Prefix - Python
Views: 9 | Author: abh
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        p = ""
        i = 0
        while True:
            b = False
            cs = set()
            for s in strs:
                if i >= len(s):
                    b = True
                else:
                    cs.add(s[i])
            if b:
                break
            if len(cs) > 1:
                break
            p += s[i]
            i += 1
        return p

Comments