Skip to content

Commit 1361498

Browse files
authoredMay 10, 2021
Update 131-palindrome-partitioning.js
1 parent f8094d9 commit 1361498

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎131-palindrome-partitioning.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,40 @@ function isPalindrome(str, start, i) {
3131
}
3232
return true
3333
}
34+
35+
// another
36+
37+
/**
38+
* @param {string} s
39+
* @return {string[][]}
40+
*/
41+
const partition = function(s) {
42+
const res = []
43+
helper(s, 0, [], res)
44+
return res
45+
};
46+
47+
function helper(str, idx, cur, res) {
48+
if(idx >= str.length) {
49+
res.push(cur.slice())
50+
return
51+
}
52+
for(let i = idx, len = str.length; i < len; i++) {
53+
const tmp = str.slice(idx, i + 1)
54+
if(chk(tmp)) {
55+
cur.push(tmp)
56+
helper(str, i + 1, cur, res)
57+
cur.pop()
58+
}
59+
}
60+
}
61+
function chk(str) {
62+
const n = str.length
63+
let l = 0, r = n - 1
64+
while(l < r) {
65+
if(str[l] !== str[r]) return false
66+
l++
67+
r--
68+
}
69+
return true
70+
}

0 commit comments

Comments
 (0)
Please sign in to comment.