Skip to content

Commit ffa0e87

Browse files
committed
palindromic-substrings solution
1 parent 341a991 commit ffa0e87

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

palindromic-substrings/moonjonghoo.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var countSubstrings = function (s) {
6+
let count = 0;
7+
8+
const expand = (left, right) => {
9+
while (left >= 0 && right < s.length && s[left] === s[right]) {
10+
count++;
11+
left--;
12+
right++;
13+
}
14+
};
15+
16+
for (let i = 0; i < s.length; i++) {
17+
expand(i, i); // 홀수 길이 중심 (예: "aba")
18+
expand(i, i + 1); // 짝수 길이 중심 (예: "aa")
19+
}
20+
21+
return count;
22+
};

0 commit comments

Comments
 (0)