Skip to content

Commit 7c47e34

Browse files
committed
palindromic substrings solution
1 parent 1bd2030 commit 7c47e34

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var countSubstrings = function (s) {
2+
let result = 0;
3+
// ๊ฐœ์ˆ˜๋ฅผ ํ‚ค์›Œ๋‚˜๊ฐ€๋ฉฐ, ๊ฐ ์ž๋ฆฌ๊ฐ€ ๋Œ€์นญ์„ ์ด๋ฃจ๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.
4+
5+
// substring์˜ ๊ฐœ์ˆ˜ ์„ค์ •
6+
for (let i = 0; i < s.length; i++) {
7+
// ์‹œ์ž‘์  ์„ค์ •
8+
for (let j = 0; j < s.length - i; j++) {
9+
let isPalindromic = true;
10+
// ๋Œ€์นญ๋˜๋Š” ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋น„๊ต
11+
for (let k = j; k < Math.ceil((j * 2 + i) / 2); k++) {
12+
if (s[k] !== s[j * 2 + i - k]) {
13+
isPalindromic = false;
14+
break;
15+
}
16+
}
17+
if (isPalindromic) result += 1;
18+
}
19+
}
20+
21+
return result;
22+
};
23+
24+
// TC : o(n^3) | SC : o(1)

0 commit comments

Comments
ย (0)