class Solution {
let telDic : [String:[String]] = ["2":["a","b","c"],"3":["d","e","f"],"4":["g","h","i"],
"5":["j","k","l"],"6":["m","n","o"],"7":["p","r","q","s"],"8":["t","u","v"],"9":["w","x","y","z"]]
var comArr = [[String]]()
var reslutArr = [String]()
func letterCombinations(_ digits: String) -> [String] {
for i in digits {
if let relust = telDic[String(i)] {
comArr.append(relust)
}
}
if (comArr.count > 0) {
self.addComb(arr: comArr[0], str: String(), end: 0)
}
return reslutArr
}
func addComb( arr : [String] , str : String , end : Int){
if(end == comArr.count){
reslutArr.append(str)
return
}
var endIndex = end
for i in arr {
endIndex = end + 1
let newstr = str + i
if(endIndex == comArr.count){
addComb(arr: comArr[endIndex-1], str: newstr, end: endIndex)
}else{
addComb(arr: comArr[endIndex], str: newstr, end: endIndex)
}
}
}
}
网友评论