Palindrome Partitioning
Description
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
Solution(javascript)
const partition = (s = '') => {
const isPalindrome = (left, right) => {
if (left === right) {
return true
}
for (let i = left; i <= right; i++) {
if (s[i] !== s[right - i + left]) {
return false
}
}
return true
}
const result = []
const aux = (index = 0, current = []) => {
if (index === s.length) {
result.push(current)
}
for (let i = index; i < s.length; i++) {
if (isPalindrome(index, i)) {
aux(i + 1, [...current, s.substring(index, i + 1)])
}
}
}
aux()
return result
}