LoginSignup
0
0

More than 3 years have passed since last update.

Leetcode #609: Find Duplicate File in System

Posted at
func findDuplicate(_ paths: [String]) -> [[String]] {
    var dict = [String: [String]]()
    for path in paths {
        let components = path.components(separatedBy: " ")
        for i in stride(from: 1, to: components.count, by: 1) {
            var file = components[0] + "/"
            var str = Array(components[i])
            var index = 0
            while str[index] != "(" {
                file += "\(str[index])"
                index += 1
            }
            index += 1
            var content = ""
            while str[index] != ")" {
                content += "\(str[index])"
                index += 1
            }
            if let value = dict[content] {
                dict[content] = value + [file]
            } else {
                dict[content] = [file]
            }
        }
    }
    var answer = [[String]]()
    for key in dict.keys {
        if dict[key]!.count != 1 {
            answer.append(dict[key]!)
        }
    }
    return answer
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0