Skip to content

Commit 9e34767

Browse files
committed
palindromic-substrings solution
1 parent 761a237 commit 9e34767

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

palindromic-substrings/byol-han.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/palindromic-substrings/submissions/1644425061/
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
var countSubstrings = function (s) {
7+
let count = 0;
8+
9+
// Helper function to expand around the center
10+
function expandAroundCenter(left, right) {
11+
while (left >= 0 && right < s.length && s[left] === s[right]) {
12+
count++; // Found a palindrome
13+
left--;
14+
right++;
15+
}
16+
}
17+
18+
for (let i = 0; i < s.length; i++) {
19+
expandAroundCenter(i, i); // Odd-length palindromes
20+
expandAroundCenter(i, i + 1); // Even-length palindromes
21+
}
22+
23+
return count;
24+
};

0 commit comments

Comments
 (0)