File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 486
486
643|[ Maximum Average Subarray I] ( ./0643-maximum-average-subarray-i.js ) |Easy|
487
487
645|[ Set Mismatch] ( ./0645-set-mismatch.js ) |Medium|
488
488
646|[ Maximum Length of Pair Chain] ( ./0646-maximum-length-of-pair-chain.js ) |Medium|
489
+ 647|[ Palindromic Substrings] ( ./0647-palindromic-substrings.js ) |Medium|
489
490
648|[ Replace Words] ( ./0648-replace-words.js ) |Medium|
490
491
649|[ Dota2 Senate] ( ./0649-dota2-senate.js ) |Medium|
491
492
653|[ Two Sum IV - Input is a BST] ( ./0653-two-sum-iv-input-is-a-bst.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments