leetcode系列–第131题.分割字符串
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
输入:s = "aab"
输出:[["a","a","b"],["aa","b"]]
输入:s = "a"
输出:[["a"]]
var partition = function (s) {
let length = s.length;
let result = []
let path = []
partitionHelper(s, 0)
return result
function partitionHelper(str, startIndex) {
if (startIndex === length) {
console.log(path)
result.push([...path])
return
}
for (let i = startIndex; i < length; i++) {
let temp = str.substr(startIndex, i - startIndex + 1)
if (!isPalindrome(temp)) {
continue
}
path.push(temp)
partitionHelper(str, i + 1)
path.pop()
}
}
function isPalindrome(str) {
let l = 0,
r = str.length - 1;
while (l < r) {
if (str[l] != str[r]) {
return false
}
l++
r--
}
return true
}
};