Swift solution, 52 ms


  • 0
    S

    Beats only 71% of results, I'm wondering how to make it faster

    class Solution {
        
        func longestCommonPrefix(_ strs: [String]) -> String {
            if strs.isEmpty {
                return ""
            }
            
            let first = strs[0]
            let start = first.startIndex
            var end = first.startIndex
            
            while strs.reduce(true, { $0 && $1.endIndex != end && first[end] == $1[end] }) {
                end = first.index(end, offsetBy: 1)
            }
            
            return first[start..<end]
        }
    }
    

Log in to reply
 

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