Skip to content

Commit 0ae4a82

Browse files
committed
palindromic-substrings solution
1 parent 66b6fe1 commit 0ae4a82

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

palindromic-substrings/lhc0506.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
6+
var countSubstrings = function(s) {
7+
let count = 0;
8+
for (let i = 0; i < s.length; i++) {
9+
let start = i;
10+
let end = i;
11+
while (start >= 0 && end < s.length && s[start] === s[end]) {
12+
count += 1;
13+
start -= 1;
14+
end += 1;
15+
}
16+
17+
start = i;
18+
end = i + 1;
19+
while (start >= 0 && end < s.length && s[start] === s[end]) {
20+
count += 1;
21+
start -= 1;
22+
end += 1;
23+
}
24+
}
25+
26+
return count;
27+
};
28+
29+
// 시간 복잡도: O(n^2)
30+
// 공간 복잡도: O(1)

0 commit comments

Comments
 (0)