-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromic-substrings.js
More file actions
52 lines (41 loc) · 1.1 KB
/
palindromic-substrings.js
File metadata and controls
52 lines (41 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Problem: Palindromic Substrings
* Link: https://leetcode.com/problems/palindromic-substrings/
* Difficulty: Medium
*
* Count the number of palindromic substrings.
*
* Example: "aaa" => 6 ("a","a","a","aa","aa","aaa")
*
* Time Complexity: O(n^2)
* Space Complexity: O(1)
*/
// JavaScript Solution - Expand Around Center
function countSubstrings(s) {
let count = 0;
function expandAroundCenter(left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
count++;
left--; right++;
}
}
for (let i = 0; i < s.length; i++) {
expandAroundCenter(i, i); // odd-length palindromes
expandAroundCenter(i, i + 1); // even-length palindromes
}
return count;
}
module.exports = countSubstrings;
/* Python Solution:
def countSubstrings(s):
count = 0
def expand(left, right):
nonlocal count
while left >= 0 and right < len(s) and s[left] == s[right]:
count += 1
left -= 1; right += 1
for i in range(len(s)):
expand(i, i) # odd
expand(i, i + 1) # even
return count
*/