Leetcode 17 Letter Combinations of a Phone Number Solution in java | Hindi Coding Community

0

 


Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. 


Example 1:


Input: digits = "23"

Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Example 2:


Input: digits = ""

Output: []




class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if(digits.length() ==0) return res;
HashMap<String,String> map = new HashMap<>();
map.put("2","abc");
map.put("3","def");
map.put("4","ghi");
map.put("5","jkl");
map.put("6","mno");
map.put("7","pqrs");
map.put("8","tuv");
map.put("9","wxyz");
Deque<String> dq = new ArrayDeque();
dq.add("");
for(int i=0;i<digits.length();i++){
String d = digits.substring(i,i+1);
String fromMap = map.get(d);
int n = dq.size();
for(int j=0;j<n;j++){
String pull = dq.pollFirst();
for(int k =0;k<fromMap.length();k++){
String temp = pull.concat(fromMap.charAt(k)+"");
dq.add(temp);
}

}

}
while(!dq.isEmpty()){
res.add(dq.poll());
}
return res;
}
}


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !