Leetcode 131 Solution

This article provides solution to leetcode question 131 (palindrome-partitioning)

https://leetcode.com/problems/palindrome-partitioning

Solution

class Solution {
    vector<vector<string>> m_res;
public:
    void partition(const string& s, int i, vector<string>& res)
    {
        if (i == s.size())
        {
            m_res.push_back(res);
            return;
        }

        for (int j = i; j < s.size(); j++)
        {
            if (s[i] != s[j])
                continue;

            int k1 = i;
            int k2 = j;

            while (k1 < k2 && s[k1] == s[k2])
                k1++, k2--;

            if (k1 >= k2)
            {
                res.push_back(s.substr(i, j - i + 1));
                partition(s, j + 1, res);
                res.pop_back();
            }
        }
    }

    vector<vector<string>> partition(string s) {
        vector<string> res;
        partition(s, 0, res);
        return m_res;
    }
};