Skip to content

Commit 345c2d0

Browse files
committed
add solution for Palindromic-substrings
1 parent 75fe368 commit 345c2d0

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
//길이 1~s.length()까지 palindrome 함수 시도
3+
int cnt=0; //
4+
5+
6+
public int countSubstrings(String s) {
7+
8+
for(int len=1; len <= s.length();len++){//1~3
9+
for(int idx =0; idx <= s.length()-len ; idx++){
10+
//idx인덱스부터 len길이만큼의 문자열이 palindrome인지 판별
11+
boolean result = isPalindrome(s, idx, idx+len-1); //start: 시작 인덱스 , end: 끝 인덱스
12+
13+
if (result){
14+
cnt++;
15+
}
16+
}
17+
}
18+
19+
return cnt;
20+
21+
}
22+
23+
private static boolean isPalindrome(String w, int start, int end){
24+
if (start == end) return true;
25+
//start!= end
26+
int s = start;
27+
int e = end;
28+
while(s<= e){
29+
if(e < start && s > end) return true;
30+
31+
32+
//서로 값이 다른 경우
33+
if (w.charAt(s) != w.charAt(e)){
34+
return false;
35+
}
36+
37+
//서로 값이 같은 경우 continue;
38+
s++;
39+
e--;
40+
}
41+
42+
43+
return true;
44+
}
45+
46+
47+
}
48+
49+

0 commit comments

Comments
 (0)