Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let map : [Character : String] = [ "2" : "abc",
- "3" : "def",
- "4" : "ghi",
- "5" : "jkl",
- "6" : "mno",
- "7" : "pqrs",
- "8" : "tuv",
- "9" : "wxyz"]
- func letterCombinations(_ digits: String) -> [String] {
- guard digits.count > 0 else { return [String]()}
- let arr = Array(digits)
- var list = [Character]()
- var res = [String]()
- combinations(arr, 0, &list, &res)
- return res
- }
- func combinations(_ arr:[Character], _ index: Int, _ list: inout [Character], _ res: inout [String]){
- if list.count == arr.count{
- res.append(String(list))
- return
- }
- if let currList = map[arr[index]]{
- for char in currList{
- list.append(char)
- combinations(arr, index + 1, &list, &res)
- list.removeLast()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement