Published Mar 27, 2022
[
 
]
Given a string s
, partition s
such that every substring of the partition
is a palindrome. Return all possible palindrome partitioning of s
.
A palindrome string is a string that reads the same backward as forward.
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Input: s = "a"
Output: [["a"]]
function partition(s: string): string[][] {
let res: string[][] = [];
let part: string[] = [];
const isPalindrome = (str: string, left: number, right: number): boolean => {
while(left < right){
if(str[left] !== str[right]){
return false;
}
left++;
right--;
}
return true;
}
const dfs = (i: number): void => {
if(i >= s.length){
res.push([...part])
return
}
for(let j = i; j < s.length; j++){
if(isPalindrome(s, i, j)){
part.push(s.substring(i, j + 1));
dfs(j + 1)
part.pop()
}
}
}
dfs(0)
return res;
};