Swift Solution in One Line


  • 0
    U

    Uses Horizontal Scanning.

    class Solution {
        func longestCommonPrefix(_ strs: [String]) -> String {
            return strs.count == 0 ? "" :
            strs.dropFirst().reduce(strs[0]) { (acc, item) -> String in
                String(zip(acc.characters, item.characters).prefix(while: { $0 == $1}).map({$0.0}));
            }
        }
    }
    

Log in to reply
 

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