---
# System prepended metadata

title: LC 131. Palindrome Partitioning
tags: [medium, leedcode, Backtracking, python, c++]

---

# LC 131. Palindrome Partitioning

### [Problem link](https://leetcode.com/problems/palindrome-partitioning/)

###### tags: `leedcode` `python` `c++` `medium` `Backtracking`

Given a string <code>s</code>, partition <code>s</code> such that every **substring** of the partition is a **palindrome** . Return all possible palindrome partitioning of <code>s</code>.

**Example 1:** 
```
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
```
**Example 2:**
```
Input: s = "a"
Output: [["a"]]
```

**Constraints:** 

- <code>1 <= s.length <= 16</code>
- <code>s</code> contains only lowercase English letters.

## Solution 1 - Backtracking
#### Python
```python=
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        path = []
        res = []

        def is_palindrome(left, right):
            while left < right:
                if s[left] != s[right]:
                    return False
                left += 1
                right -= 1
            return True

        def backtrack(startIdx):
            if startIdx == len(s):
                res.append(path[:])
                return

            for i in range(startIdx, len(s)):
                if is_palindrome(startIdx, i):
                    path.append(s[startIdx : i + 1])
                    backtrack(i + 1)
                    path.pop()

        backtrack(0)

        return res
```
#### C++
```cpp=
class Solution {
public:
    vector<vector<string>> res;

    bool isPalindrome(string &s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    }

    void backtracking(string &s, vector<string> &path, int startIdx) {
        if (startIdx == s.size()) {
            res.push_back(path);
        }
        for (int i = startIdx; i < s.size(); i++) {
            if (isPalindrome(s, startIdx, i)) {
                path.push_back(s.substr(startIdx, i - startIdx + 1));
                backtracking(s, path, i + 1);
                path.pop_back();
            }
        }
    }

    vector<vector<string>> partition(string s) {
        vector<string> path;
        backtracking(s, path, 0);
        return res;
    }
};
```
```cpp=
class Solution {
public:
    bool isPalid(string& s, int left, int right) {
        while (left < right) {
            if (s[left] != s[right]) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    vector<vector<string>> partition(string s) {
        int n = s.size();
        vector<vector<string>> ans;
        vector<string> path;

        function<void(int)> dfs = [&](int i) {
            if (i == n) {
                ans.push_back(path);
                return;
            }
            for (int j = i; j < n; j++) {
                if (isPalid(s, i, j)) {
                    path.push_back(s.substr(i, j - i + 1));
                    dfs(j + 1);
                    path.pop_back();
                }
            }
        };
        dfs(0);
        return ans;
    }
};
```

>### Complexity
>n = s.length
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n*($2^n$))    | O($2^n$)         |


## Note
[代碼隨想錄](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0131.%E5%88%86%E5%89%B2%E5%9B%9E%E6%96%87%E4%B8%B2.md)
