One question about submmison


  • 0
    H

    Hi, guys. I am new to python and this is my solution. However, when I submit, for the input ["c","c"], it shows the output is empty. I test this code in Ideone, but Ideone shows the result is "c". Can someone tell me what is wrong?

    class Solution(object):
        def longestCommonPrefix(self, strs):
            """
            :type strs: List[str]
            :rtype: str
            """
            if len(strs) == 0:
                return ""
            else:
                L = len(strs[0])
                i = 0
                s = 0
                while i < L:
                    try:
                        for str in strs:
                            if str[i] == strs[0][i]:
                                s += 1/(len(strs))
                        if s == 1:
                            i += 1
                            s = 0
                        else:
                            break
                    except:
                        break
    
                if i==0:
                    return ""
                else:
                    return strs[0][:i]
    

Log in to reply
 

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