<p>Given a string <code>s</code>, partition <code>s</code> such that every <span data-keyword="substring-nonempty">substring</span> of the partition is a <span data-keyword="palindrome-string"><strong>palindrome</strong></span>. Return <em>all possible palindrome partitioning of </em><code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aab"
<strong>Output:</strong> [["a","a","b"],["aa","b"]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "a"
<strong>Output:</strong> [["a"]]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 16</code></li>
	<li><code>s</code> contains only lowercase English letters.</li>
</ul>

class Solution {
public:
    vector<vector<string>> partition(string s) {
        vector<vector<string>> result;
        vector<int> curr_partitions;
        backtrack(1, s, curr_partitions, result);
        return result;
    }

private:
    void backtrack(int start, const string& s, vector<int>& current, vector<vector<string>>& result) {
        vector<string> partitioned_str = get_partitioned_string(s, current);
        if(!partitioned_str.empty())
            result.push_back(partitioned_str);
        
        for (int i = start; i < s.size(); i++) {
            current.push_back(i);
            backtrack(i + 1, s, current, result);
            current.pop_back();  // This is the backtracking step
        }
    }

    // vector<string> get_partitioned_string(const string& s, vector<int>& partitions) {
    //     // if(partitions.empty() && isPalindrome(s)) return {s};
    //     vector<string> partitioned_string;
    //     int prev_partition = 0;
    //     for(int i = 0; i < partitions.size(); i++) {
    //         string substring = s.substr(prev_partition, partitions[i]); THIS IS NOT HOW YOU GET SUBSTRING!!
    //         string substring = s.substr(prev_partition, );
    //         if(!isPalindrome(substring)) return {};
            
    //         prev_partition = partitions[i];
    //     }
    //     string last_substring = s.substr(prev_partition, s.size());
    //     if(isPalindrome(last_substring)) 
    //         partitioned_string.push_back(last_substring);
    //     else return {};
    //     return partitioned_string;
    // }

    vector<string> get_partitioned_string(const string& s, vector<int>& partitions) {
        vector<string> partitioned_string;
        int prev_partition = 0;
        for(int i = 0; i < partitions.size(); i++) {
            string substring = s.substr(prev_partition, partitions[i] - prev_partition);
            if(!isPalindrome(substring)) return {};
            partitioned_string.push_back(substring);
            prev_partition = partitions[i];
        }
        string last_substring = s.substr(prev_partition, s.size() - prev_partition);
        if(isPalindrome(last_substring)) 
            partitioned_string.push_back(last_substring);
        else return {};
        return partitioned_string;
    }
    
    bool isPalindrome(const string& s) {
        int l = 0;
        int r = s.size() - 1;
        while (l <= s.size()/2) {
            if(s[l] != s[r]) return false;
            l++;
            r--;
        }
        return true;
    }
};
123390