forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.cpp
More file actions
34 lines (29 loc) · 726 Bytes
/
Copy pathobzva.cpp
File metadata and controls
34 lines (29 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* For the length N of given string s,
*
* Time complexity: O(N^3)
*
* Space complexity: O(1)
*/
class Solution {
public:
int countSubstrings(string s) {
int res = 0;
for (int i = 0; i < s.size(); i++) {
for (int j = i; j < s.size(); j++) {
int start = i, end = j;
bool is_palindrome = true;
while (start <= end) {
if (s[start] != s[end]) {
is_palindrome = false;
break;
}
start++;
end--;
}
if (is_palindrome) res++;
}
}
return res;
}
};