Skip to content

Commit 52b285e

Browse files
committed
Add palindromic substrings Solution - s0ooo0k
1 parent a471acf commit 52b285e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
// 팰린드롬의 경우 양쪽이 같아야 하므로, 중심을 잡고 left, right로 늘려나가는 방식
3+
public int countSubstrings(String s) {
4+
int cnt = 0;
5+
6+
for(int i=0; i<s.length(); i++) {
7+
cnt += palindrom(s, i, i);
8+
cnt += palindrom(s, i, i+1);
9+
}
10+
return cnt;
11+
}
12+
public int palindrom(String s, int left, int right) {
13+
int cnt = 0;
14+
while(left>=0 && right < s.length() && s.charAt(left)==s.charAt(right)) {
15+
cnt++;
16+
left--;
17+
right++;
18+
}
19+
return cnt;
20+
}
21+
}
22+

0 commit comments

Comments
 (0)