c++ backtracking solution


  • 0
    B
    class Solution {
    public:
        vector<string> result;
        vector<string> dic={" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        
        void helper(string s,string cur){
            if(!s.size()) {
                result.push_back(cur);
                return;
            }    
            
            int n=s[0]-'0';
            for(char c:dic[n]){
                string temp=cur+c;
                helper(s.substr(1,s.size()-1),temp);
            }
            
        }
        
        vector<string> letterCombinations(string digits) {
            if(!digits.size()) return result;
            helper(digits,"");
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.