Skip to content

Commit 381f802

Browse files
committed
refacotr: rename variables more descriptive
1 parent 2ad5c0e commit 381f802

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

palindromic-substrings/tolluset.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,40 @@
33
* SC: O(n)
44
* */
55
function countSubstrings(s: string): number {
6-
const str = "#" + s.split("").join("#") + "#";
7-
const len = str.length;
8-
const pal = new Array(len).fill(0);
9-
let center = 0,
10-
radius = 0,
11-
total = 0;
6+
const transformedString = "#" + s.split("").join("#") + "#";
7+
const transformedStringLength = transformedString.length;
8+
const palindromeLengths = new Array(transformedStringLength).fill(0);
9+
let currentCenter = 0,
10+
rightBoundary = 0,
11+
totalPalindromeCount = 0;
1212

13-
for (let i = 0; i < len; i++) {
13+
for (let i = 0; i < transformedStringLength; i++) {
1414
// If i is within the rightmost center, copy the palindromes value from the mirror
15-
if (i < radius) {
16-
pal[i] = Math.min(radius - i, pal[center * 2 - i]);
15+
if (i < rightBoundary) {
16+
palindromeLengths[i] = Math.min(
17+
rightBoundary - i,
18+
palindromeLengths[currentCenter * 2 - i],
19+
);
1720
}
1821

1922
// Expand around i until it's not a palindrome and not over left or right
2023
while (
21-
i + pal[i] + 1 < len &&
22-
i - pal[i] - 1 >= 0 &&
23-
str[i + pal[i] + 1] === str[i - pal[i] - 1]
24+
i + palindromeLengths[i] + 1 < transformedStringLength &&
25+
i - palindromeLengths[i] - 1 >= 0 &&
26+
transformedString[i + palindromeLengths[i] + 1] ===
27+
transformedString[i - palindromeLengths[i] - 1]
2428
) {
25-
pal[i]++;
29+
palindromeLengths[i]++;
2630
}
2731

2832
// If palindromes value is the new rightmost center, update center and right
29-
if (i + pal[i] > radius) {
30-
center = i;
31-
radius = i + pal[i];
33+
if (i + palindromeLengths[i] > radius) {
34+
currentCenter = i;
35+
rightBoundary = i + palindromeLengths[i];
3236
}
3337

3438
// Add the number of palindromes with center i to the result
35-
total += Math.floor((pal[i] + 1) / 2);
39+
total += Math.floor((palindromeLengths[i] + 1) / 2);
3640
}
3741

3842
return total;

0 commit comments

Comments
 (0)