Skip to content

Instantly share code, notes, and snippets.

@fengyitsai
Created February 11, 2020 12:53
Show Gist options
  • Save fengyitsai/61124cb62f73fcad7640d7e223576f20 to your computer and use it in GitHub Desktop.
Save fengyitsai/61124cb62f73fcad7640d7e223576f20 to your computer and use it in GitHub Desktop.
791. Custom Sort String
class Solution {
func customSortString(_ S: String, _ T: String) -> String {
let sArray = Array(S)
var counter = [Character:Int]()
for char in T {
counter[char, default:0] += 1
}
var result = [Character]()
for char in S {
guard let count = counter[char] else {
continue
}
result.append(contentsOf: [Character](repeating:char, count:count))
counter[char] = nil
}
for char in T {
guard let count = counter[char] else {
continue
}
result.append(contentsOf: [Character](repeating:char, count:count))
counter[char] = nil
}
return String(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment