-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path131. Palindrome Partitioning.js
72 lines (59 loc) · 1.43 KB
/
131. Palindrome Partitioning.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
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"]
]
*/
/**
* Algorithm: Backtracking
* Note:
* 1. Since we are going to use s.substring(index, i), the i upper boundary === s.length (Tricky)
*
*/
/**
* @param {string} s
* @return {string[][]}
*/
var partition = function(s) {
let result = [];
if (s === null || s.length === 0) {
return result;
}
let path = [];
function helper(index) {
if (index === s.length) {
result.push([...path]);
return;
}
for (let i = index + 1; i <= s.length; i++) {
let prefix = s.substring(index, i)
if (!isPalindrome(prefix)) {
continue;
}
path.push(prefix);
helper(i);
path.splice(path.length - 1, 1);
}
}
function isPalindrome(prefix) {
for (let i = 0; i < prefix.length / 2; i++) {
if (prefix[i] !== prefix[prefix.length - 1 - i]) {
return false;
}
}
return true;
}
helper(0);
return result;
};
let test1 = 'a'; // ['a']
let test2 = 'ab'; // ['a', 'b']
let test3 = 'aab'; //[['a', 'a', 'b'], ['aa', 'b']]
console.log(partition(test1));
console.log(partition(test2));
console.log(partition(test3));