Skip to content

Commit 84e8605

Browse files
committed
palindromic-substrings solution
1 parent 097eb44 commit 84e8605

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param s - λ¬Έμžμ—΄
3+
* @returns - 회문 (λ’€μ§‘μ–΄ 읽어도 같은 λ¬Έμžμ—΄) 의 갯수 λ°˜ν™˜
4+
* @description
5+
* - 같은 κΈ€μžμ—¬λ„ 각각은 λ”°λ‘œ νŒλ‹¨ν•˜λ©° ν•œ κΈ€μžλ„ 회문으둜 νŒλ‹¨
6+
*/
7+
8+
function countSubstrings(s: string): number {
9+
let n = s.length;
10+
let cnt = 0;
11+
const dp: boolean[][] = Array.from({ length: n }, () => Array(n).fill(false));
12+
13+
for (let j = 0; j < s.length; j++) {
14+
for (let i = 0; i <= j; i++) {
15+
if (s[i] === s[j] && (j - i <= 2 || dp[i + 1][j - 1])) {
16+
dp[i][j] = true;
17+
cnt++;
18+
}
19+
}
20+
}
21+
return cnt;
22+
}
23+
24+
const s = "aaa";
25+
countSubstrings(s);

0 commit comments

Comments
Β (0)