Skip to content

Commit 1d1ceca

Browse files
committed
Palindromic Substrings
1 parent b776697 commit 1d1ceca

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

palindromic-substrings/hyejjun.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var countSubstrings = function (s) {
6+
let count = 0;
7+
8+
function checkPalindromic(left, right) {
9+
while (left >= 0 && right < s.length && s[left] === s[right]) {
10+
count++;
11+
left--;
12+
right++;
13+
}
14+
15+
}
16+
17+
for (let i = 0; i < s.length; i++) {
18+
checkPalindromic(i, i);
19+
checkPalindromic(i, i + 1);
20+
}
21+
22+
return count;
23+
};
24+
25+
console.log(countSubstrings("abc"));
26+
console.log(countSubstrings("aaa"));
27+
28+
29+
/*
30+
Time Complexity : O(n^2)
31+
Space Complexity: O(1)
32+
*/

0 commit comments

Comments
 (0)