6 line simple Python

  • 2

    First find the length of shortest str, and than iterate to find qualified prefix

    def longestCommonPrefix(self, strs):
        minL = min(map(len, strs)) if strs else 0
        for i in range(minL):
            for j in range(1, len(strs)):
                if strs[j][i] != strs[0][i]:
                    return strs[0][:i]
        return strs[0][:minL] if minL else ""

  • 0

    very clear idea and easy to follow, thank you

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.