Skip to content

Commit a58fc6c

Browse files
committed
add 647 palindromic-substrings.
1 parent be3ea55 commit a58fc6c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

647-palindromic-substrings.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const countSubstrings = function(s) {
6+
let count = 0
7+
8+
if (s == null || s.length === 0) {
9+
return 0
10+
}
11+
12+
for (let i = 0; i < s.length; i++) {
13+
extendPalindrome(s, i, i)
14+
extendPalindrome(s, i, i + 1)
15+
}
16+
17+
function extendPalindrome(str, left, right) {
18+
while (left >= 0 && right < s.length && s.charAt(left) === s.charAt(right)) {
19+
count++;
20+
left--;
21+
right++;
22+
}
23+
}
24+
return count
25+
};
26+
27+
console.log(countSubstrings("abc"))
28+
console.log(countSubstrings("aaa"))

0 commit comments

Comments
 (0)