Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ece065a

Browse files
committedAug 13, 2024
Add week 1 solutions: palindromic-substrings
1 parent 029599b commit ece065a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 

‎palindromic-substrings/gitsunmin.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/palindromic-substrings/
3+
* time complexity : O(n^2)
4+
* space complexity : O(n)
5+
*/
6+
7+
const expandAroundCenter = (s: string, left: number, right: number): number => {
8+
if (left < 0 || right >= s.length || s[left] !== s[right]) {
9+
return 0;
10+
}
11+
return 1 + expandAroundCenter(s, left - 1, right + 1);
12+
};
13+
14+
function countSubstrings(s: string): number {
15+
16+
return [...Array(s.length).keys()].reduce((totalCount, i) => {
17+
return totalCount +
18+
expandAroundCenter(s, i, i) +
19+
expandAroundCenter(s, i, i + 1);
20+
}, 0);
21+
};

0 commit comments

Comments
 (0)
Please sign in to comment.