Skip to content

Commit 2d6c164

Browse files
committed
solve: palindrome-substrings
1 parent c4f4785 commit 2d6c164

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int countSubstrings(String s) {
3+
/**
4+
각 문자를 중간으로 갖는 palindrome 여부 체크
5+
+ 두개의 문자를 중간으로 갖는 palindrome 여부 체크
6+
*/
7+
int count = 0;
8+
int length = s.length();
9+
for (int i = 0; i < length; i++) { // O(N)
10+
int start = i;
11+
int end = i;
12+
while (0 <= start && end < length && start <= end && s.charAt(start) == s.charAt(end)) { // O(N)
13+
count++;
14+
start--;
15+
end++;
16+
}
17+
18+
start = i;
19+
end = i + 1;
20+
while (0 <= start && end < length && start <= end && s.charAt(start) == s.charAt(end)) { // O(N)
21+
count++;
22+
start--;
23+
end++;
24+
}
25+
}
26+
27+
return count; // O(N^2)
28+
}
29+
}

0 commit comments

Comments
 (0)