Skip to content

Commit cfed41a

Browse files
committedMar 8, 2025
Add solution #647
1 parent 342c0c3 commit cfed41a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@
486486
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
487487
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|
488488
646|[Maximum Length of Pair Chain](./0646-maximum-length-of-pair-chain.js)|Medium|
489+
647|[Palindromic Substrings](./0647-palindromic-substrings.js)|Medium|
489490
648|[Replace Words](./0648-replace-words.js)|Medium|
490491
649|[Dota2 Senate](./0649-dota2-senate.js)|Medium|
491492
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 647. Palindromic Substrings
3+
* https://leetcode.com/problems/palindromic-substrings/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, return the number of palindromic substrings in it.
7+
*
8+
* A string is a palindrome when it reads the same backward as forward.
9+
*
10+
* A substring is a contiguous sequence of characters within the string.
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @return {number}
16+
*/
17+
var countSubstrings = function(s) {
18+
let result = 0;
19+
20+
for (let i = 0; i < s.length; i++) {
21+
result += expand(i, i);
22+
result += expand(i, i + 1);
23+
}
24+
25+
return result;
26+
27+
function expand(left, right) {
28+
let count = 0;
29+
while (left >= 0 && right < s.length && s[left] === s[right]) {
30+
count++;
31+
left--;
32+
right++;
33+
}
34+
return count;
35+
}
36+
};

0 commit comments

Comments
 (0)