File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments