Skip to content

Commit 68bc62b

Browse files
authored
Create yeongu.cpp
1 parent d4c79cc commit 68bc62b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

palindromic-substrings/yeongu.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// TC: O(n^2) For문 안에서 while문 사용하는 함수 호출
2+
// SC: O(1)
3+
4+
class Solution {
5+
public:
6+
int find_palindrome1(string& original_text, int index) {
7+
int cnt = 0;
8+
int left_ptr = index, right_ptr = index;
9+
10+
while (left_ptr >= 0 and right_ptr < original_text.size() and
11+
original_text[left_ptr] == original_text[right_ptr]) {
12+
cnt += 1;
13+
left_ptr -= 1;
14+
right_ptr += 1;
15+
}
16+
return cnt;
17+
}
18+
19+
int find_palindrome2(string& original_text, int index) {
20+
int cnt = 0;
21+
int left_ptr = index, right_ptr = index + 1;
22+
23+
while (left_ptr >= 0 and right_ptr < original_text.size() and
24+
original_text[left_ptr] == original_text[right_ptr]) {
25+
cnt++;
26+
left_ptr--;
27+
right_ptr++;
28+
}
29+
return cnt;
30+
}
31+
32+
int countSubstrings(string& s) {
33+
int output = 0;
34+
for (int i = 0; i < s.size(); i++) {
35+
output += find_palindrome1(s, i);
36+
}
37+
for (int i = 0; i < s.size() - 1; i++) {
38+
output += find_palindrome2(s, i);
39+
}
40+
return output;
41+
}
42+
};

0 commit comments

Comments
 (0)