-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindromePartitioningII.js
83 lines (74 loc) · 2.88 KB
/
palindromePartitioningII.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
73
74
75
76
77
78
79
80
81
82
83
function isPalindromeDP(str){
let table = [];
for(let itrRow =0; itrRow < str.length ; itrRow++) {
let rowArr = [];
for (let itrCol = 0; itrCol < str.length; itrCol++) {
rowArr.push(0);
}
table.push(rowArr);
}
for (let itrCol = 0; itrCol < str.length; itrCol++) {
for(let itrRow = 0, itrTmpCol = itrCol; itrRow<str.length && itrTmpCol<str.length; itrRow++, itrTmpCol++) {
let value = 0;
if(itrRow == itrTmpCol) {
value = 1;
} else if (itrRow < itrTmpCol) {
if (str[itrRow] == str[itrTmpCol]) {
if (itrTmpCol - itrRow == 1) {
value = 1;
} else if(table[itrRow+1][itrTmpCol-1] == 1){
value = 1;
}
}
}
console.log(itrRow+"_"+itrTmpCol)
table[itrRow][itrTmpCol] = value;
}
}
// "a" "b" "c" "b" "a"
//"a"
//"b"
//"c"
//"b"
//"a"
// 0,0 -> 1,1 -> 2,2 -> 3,3 -> 4,4
// 0,1 -> 1,2 -> 2,3 -> 3,4
// 0,2 -> 1,3 -> 2,4
// 0,3 -> 1,4
return table;
}
console.log(isPalindromeDP("abcba"))
function isPalindrome(str, startIndex, endIndex) {
if(startIndex == endIndex) return true;
let length = (endIndex - startIndex +1) /2;
length = Math.floor(length);
for(let itr = 0; itr < length; itr++ ) {
if(str[startIndex++] != str[endIndex--]) {
return false;
}
}
return true;
}
function minCuts(str, startIndex, endIndex, hashMap) {
let hashkey = startIndex + "_" + endIndex;
if(hashMap.hasOwnProperty(hashkey)) return hashMap[hashkey];
// base cases
if(isPalindrome(str, startIndex, endIndex)) return hashMap[hashkey]=0;
let minCutsCount = Infinity;
for(let itr = startIndex+1; itr <= endIndex; itr++) {
minCutsCount = Math.min( (
minCuts(str,startIndex, itr-1, hashMap) +
minCuts(str,itr, endIndex, hashMap) +
1) , minCutsCount );
}
hashMap[hashkey] = minCutsCount;
return hashMap[hashkey];
}
var minCut = function(s) {
// check if string s is a palindrome ==> return 0
if(isPalindrome(s, 0, s.length-1)) return 0;
let hashMap = {};
return minCuts(s, 0, s.length-1, hashMap);
};
// minCut("aab")
//console.log(minCut("adabdcaebdcebdcacaaaadbbcadabcbeabaadcbcaaddebdbddcbdacdbbaedbdaaecabdceddccbdeeddccdaabbabbdedaaabcdadbdabeacbeadbaddcbaacdbabcccbaceedbcccedbeecbccaecadccbdbdccbcbaacccbddcccbaedbacdbcaccdcaadcbaebebcceabbdcdeaabdbabadeaaaaedbdbcebcbddebccacacddebecabccbbdcbecbaeedcdacdcbdbebbacddddaabaedabbaaabaddcdaadcccdeebcabacdadbaacdccbeceddeebbbdbaaaaabaeecccaebdeabddacbedededebdebabdbcbdcbadbeeceecdcdbbdcbdbeeebcdcabdeeacabdeaedebbcaacdadaecbccbededceceabdcabdeabbcdecdedadcaebaababeedcaacdbdacbccdbcece"))