Why I got a wrong answer?

  • 0

    the input is ["abc","abcc","abc","abca","abca"]
    the leetcode said my output is "ab"
    but i tested my code in xcode, the output actually is "abc"
    Why I got a wrong answer in leetcode?

    class Solution {
        func longestCommonPrefix(_ strs: [String]) -> String {
            if(strs == []){
                return ""
            var commonPre = strs[0]
            for i in 1..<strs.count{
                commonPre = strs[i].commonPrefix(with: commonPre)
            return commonPre

Log in to reply

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