Skip to content

Commit 6e87588

Browse files
committed
solved1. countSubstrings
1 parent 2d98148 commit 6e87588

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*
5+
* 풀이 방법
6+
*
7+
* 1. brute force 를 사용해서 모든 경우의 수를 구한다.
8+
* 2. 투포인터를 통해 isPalindrome 을 확인한다.
9+
*
10+
* 복잡성
11+
*
12+
* Time Complexity: O(n^2)
13+
* Space Complexity: O(1)
14+
*/
15+
16+
/**
17+
* isPalindrome 함수
18+
*/
19+
function isPalindrome(s) {
20+
let left = 0;
21+
let right = s.length - 1;
22+
23+
while (left < right) {
24+
if (s[left] !== s[right]) return false;
25+
left++;
26+
right--;
27+
}
28+
29+
return true;
30+
}
31+
32+
var countSubstrings = function(s) {
33+
let count = 0;
34+
35+
// 모든 경우의 수를 구한다.
36+
for (let start = 0; start < s.length; start++) {
37+
for (let end = start; end < s.length; end++) {
38+
const subStr = s.slice(start, end + 1);
39+
40+
// isPalindrome 함수를 통해 팰린드롬인지 확인한다.
41+
if (isPalindrome(subStr)) {
42+
count++;
43+
}
44+
}
45+
}
46+
47+
return count;
48+
};

0 commit comments

Comments
 (0)