Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

Leetcode #609: Find Duplicate File in System

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
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?