-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0647-palindromic-substrings.js
More file actions
142 lines (115 loc) · 3.53 KB
/
0647-palindromic-substrings.js
File metadata and controls
142 lines (115 loc) · 3.53 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Brut Force - Check All Substrings
* Time O(N^3) | Space O(1)
* https://leetcode.com/problems/palindromic-substrings/
* @param {string} s
* @return {number}
*/
var countSubstrings = (s, count = 0) => {
for (let left = 0; left < s.length; left++) {
/* Time O(N) */
for (let right = left; right < s.length; right++) {
/* Time O(N) */
count += Number(isPalindrome(s, left, right)); /* Time O(N) */
}
}
return count;
};
const isPalindrome = (s, left, right) => {
while (left < right) {
/* Time O(N) */
const isEqual = s[left] === s[right];
if (!isEqual) return false;
left++;
right--;
}
return true;
};
/**
* DP - Bottom Up
* Matrix - Tabulation
* 2 Pointer - Slide Window
* Time O(N^2) | Space O(N^2)
* https://leetcode.com/problems/palindromic-substrings/
* @param {string} s
* @return {number}
*/
var countSubstrings = (s, count = 0) => {
const tabu = initTabu(s); /* Time O(N * N) | Space O(N * N) */
count += singleLetters(s, tabu); /* Time O(N) | Space O(N * N) */
count += doubleLetters(s, tabu); /* Time O(N) | Space O(N * N) */
count += multiLetters(s, tabu); /* Time O(N * N) | Space O(N * N) */
return count;
};
const initTabu = (s) =>
new Array(s.length)
.fill() /* Space O(N) */
.map(() => new Array(s.length).fill(false)); /* Space O(N) */
const singleLetters = (s, tabu, count = 0) => {
for (let index = 0; index < s.length; index++) {
/* Time O(N) */
tabu[index][index] = true; /* Space O(N * N) */
count += Number(tabu[index][index]);
}
return count;
};
const doubleLetters = (s, tabu, count = 0) => {
for (let curr = 0; curr < s.length - 1; curr++) {
/* Time O(N) */
const next = curr + 1;
const isEqual = s[curr] === s[next];
tabu[curr][next] = isEqual; /* Space O(N * N) */
count += Number(tabu[curr][next]);
}
return count;
};
const multiLetters = (s, tabu, count = 0) => {
for (let window = 3; window <= s.length; window++) {
/* Time O(N) */
count += slideWindow(s, tabu, window); /* Time O(N) | Space O(N * N) */
}
return count;
};
const slideWindow = (s, tabu, window, count = 0) => {
let [left, right] = [0, window - 1];
while (right < s.length) {
/* Time O(N) */
const isTrue = tabu[left + 1][right - 1];
const isEqual = s[left] === s[right];
tabu[left][right] = isTrue && isEqual; /* Space O(N * N) */
count += Number(tabu[left][right]);
left++;
right++;
}
return count;
};
/**
* 2 Pointer - Expand Around Center
* Time O(N^2) | Space O(1)
* https://leetcode.com/problems/palindromic-substrings/
* @param {string} s
* @return {number}
*/
var countSubstrings = (s, count = 0) => {
for (let i = 0; i < s.length; i++) {
/* Time O(N) */
const [odd, even] = [i, i + 1];
/* odd-length: single character center */
count += isPalindromeFromCenter(s, i, odd); /* Time O(N) */
/* even-length: consecutive characters center */
count += isPalindromeFromCenter(s, i, even); /* Time O(N) */
}
return count;
};
const isPalindromeFromCenter = (s, left, right, count = 0) => {
const isInBounds = () => 0 <= left && right < s.length;
while (isInBounds()) {
/* Time O(N) */
const isEqual = s[left] === s[right];
if (!isEqual) break;
count++;
left--;
right++;
}
return count;
};