Swift - leetcode's commonPrefix func working bad?


  • 0
    D

    So i've come up with a really basic solution in Swift, which in xcode's playground works properly, however when submitting to leetcode certain test cases won't pass, while on the playground, if i try with the same test case it gives out the expected output. Did anyone had this same problem?

        func longestCommonPrefix(_ strs: [String]) -> String {
            if strs.count == 0 {
                return ""
            }
            else {
                var prefix = strs.first!
                for element in strs {
                    prefix = prefix.commonPrefix(with: element)
                }
                return prefix
            }
        }
    

    leetcode's solution: https://imgur.com/sXJVoMR
    xcode's: https://imgur.com/F58ip2d


Log in to reply
 

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