a simple solution use map and zip


  • 0
    W

    def similar(strs):
    for i in range(0 , len(strs) - 1):
    if strs[i] == strs[i + 1]:
    continue
    else:
    return 1
    break
    return 0

    class Solution(object):
    def longestCommonPrefix(self, strs):
    if not strs:
    return ""
    ans = list(zip(*strs))
    out = []
    x = list(map(similar, ans))
    for i in range(0 , len(ans)):
    if x[i] == 0:
    out.append(ans[i][0])
    else:
    break
    out = "".join(out)
    return(out)


Log in to reply
 

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