Created
June 22, 2020 11:02
-
-
Save stevencurtis/485c483d95ea93cd0d6687d90d2261ee to your computer and use it in GitHub Desktop.
makingfilenamesunique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func getFolderNames(_ names: [String]) -> [String] { | |
var output: [String] = [] | |
var hash: [String: Int] = [:] | |
for i in 0..<names.count { | |
let name = names[i] | |
if let num = hash[name] { | |
var candidateNumber = num + 1 | |
var candidateExtension = "(\(candidateNumber))" | |
while let _ = hash[name + candidateExtension] { | |
candidateNumber += 1 | |
candidateExtension = "(\(candidateNumber))" | |
} | |
output.append(name + candidateExtension) | |
hash[name] = candidateNumber | |
hash[name + candidateExtension] = 0 | |
} else { | |
output.append(names[i]) | |
hash[names[i]] = 0 | |
} | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment