Easy to grasp C++ Solution


  • 0
    F
    class Solution {
    public:
        vector<vector<string>> partition(string s) {
            vector<vector<string>> result;
            vector<string> cur;
            help2(s, cur, result, 0);
            return result;
        }
        
        void help1(const string& s, vector<string>& cur, vector<vector<string>>& result, int pos){
            if(pos==s.size())   { result.push_back(cur);  return;}
            for(int i=pos; i<s.size(); i++){
                if(check(s, pos, i)){
                    cur.push_back(s.substr(pos, i-pos+1));
                    help1(s, cur, result, i+1);
                    cur.pop_back();
                }
            }
        }
        
        void help2(const string& s, vector<string> cur, vector<vector<string>>& result, int pos){
            if(pos==s.size())   { result.push_back(cur);  return;}
            for(int i=pos; i<s.size(); i++){
                if(check(s, pos, i)){
                    vector<string> temp(cur);
                    temp.push_back(s.substr(pos, i-pos+1));
                    help2(s, temp, result, i+1);
                }
            }
        }
        
        bool check(const string& s, int start, int end){
            while(start<end){
                if(s[start++]!=s[end--])    return false;
            }
            return true;
        }
    };

Log in to reply
 

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