Should give more time to Swift solution


  • 0
    I

    my solution :

    class Solution {
        func findDuplicate(_ paths: [String]) -> [[String]] {
            var ret : [String : Set<String>] = [:]
            for path in paths {
                let cs = path.components(separatedBy: " ")
                if let root = cs.first {
                    for i in 1..<cs.count {
                        let file = cs[i]
                        if let range1 = file.range(of: "(") {
                            if let range2 = file.range(of: ")") {
                                let name = file.substring(to: range1.lowerBound)
                                let content = file.substring(with: range1.upperBound..<range2.lowerBound)
                                if ret[content] == nil {
                                    ret[content] = Set<String>()
                                }
                                ret[content]?.insert(root + "/" + name)
                            }
                        }
                    }
                }
            }
            return ret.map({return Array($1) }).filter({return $0.count > 1})
        }
    }
    

    the idea is simple, python version get Accept, but swift version get TLE.


Log in to reply
 

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