Simple Swift solution


  • 0
    F
    func longestCommonPrefix(_ strs: [String]) -> String {
            if strs.count == 0 {
                return ""
            }
            var prefix = strs[0]
            for str in strs {
                prefix = str.commonPrefix(with: prefix)
                print(prefix)
            }
            return prefix
        }
    

    in my computer, this solution is correct.
    but when i submit, it input error.
    input: ["abc","abcc","abc","abca","abca"]
    output: "ab" (in fact, output is "abc")
    expected: "abc"
    Why?


Log in to reply
 

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