I'm a newbie in LeetCode and I'm working on the easy question: Longest Common Prefix.
I couldn't pass one of the test cases: strs = ["flower","flower","flower","flower"]
I got an output=''
Here's the question: Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
input:strs = ["flower","flower","flower","flower"] expected output: "flower"
While I checked my codes on both VSCode and Python Tutor code visualizer, I got the right output.
If someone can help me find out the bug I would be really grateful!
Appreciate it!
BTW the screenshot is my code (look ugly though :(
Feel free to give out some suggestions and solution. Thanks!
class Solution(object):
def longestCommonPrefix(self,strs):
    """
    :type strs: List[str]
    :rtype: str
    """
    length = len(strs)
    dict = {}
    for i in strs:
        for j in list(i):
            if j not in dict:
                dict[j] = 1
            else:
                dict[j] += 1
    prefix = ""
    index = 0
    for k in dict:
        if dict[k] == length:
            for char in strs:
                if  k != char[index]:
                    return prefix
            index += 1
            prefix += k
    return prefix
 
    