304 North Cardinal St.
Dorchester Center, MA 02124

# Palindrome Partitioning LeetCode Solution – Queslers

## Problem – Palindrome Partitioning

Given a string `s`, partition `s` such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of `s`.

palindrome string is a string that reads the same backward as forward.

Example 1:

``````Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]``````

Example 2:

``````Input: s = "a"
Output: [["a"]]``````

Constraints:

• `1 <= s.length <= 16`
• `s` contains only lowercase English letters.

### Palindrome Partitioning LeetCode Solution in Java

``````public List<List<String>> partition(String s) {
// Backtracking
// Edge case
if(s == null || s.length() == 0) return new ArrayList<>();

List<List<String>> result = new ArrayList<>();
helper(s, new ArrayList<>(), result);
return result;
}
public void helper(String s, List<String> step, List<List<String>> result) {
// Base case
if(s == null || s.length() == 0) {
return;
}
for(int i = 1; i <= s.length(); i++) {
String temp = s.substring(0, i);
if(!isPalindrome(temp)) continue; // only do backtracking when current string is palindrome

helper(s.substring(i, s.length()), step, result); // explore
step.remove(step.size() - 1); // unchoose
}
return;
}
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while(left <= right) {
if(s.charAt(left) != s.charAt(right))
return false;
left ++;
right --;
}
return true;
}
``````

### Palindrome Partitioning LeetCode Solution in C++

``````class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string> > ret;
if(s.empty()) return ret;

vector<string> path;
dfs(0, s, path, ret);

return ret;
}

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

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

### Palindrome Partitioning LeetCode Solution in Python

``````class Solution(object):
def __init__(self):
self.memory = collections.defaultdict(list)

def partition(self, s):
if not s: return [[]]
if s in self.memory: return self.memory[s]  # the memory trick can save some time
ans = []
for i in range(1, len(s) + 1):
if s[:i] == s[:i][::-1]:  # prefix is a palindrome
for suf in self.partition(s[i:]):  # process suffix recursively
ans.append([s[:i]] + suf)
self.memory[s] = ans
return ans``````
##### Palindrome Partitioning LeetCode Solution Review:

In our experience, we suggest you solve this Palindrome Partitioning LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Palindrome Partitioning LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Palindrome Partitioning LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions