-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountPalindromesInSubstring.java
More file actions
48 lines (37 loc) · 1.05 KB
/
countPalindromesInSubstring.java
File metadata and controls
48 lines (37 loc) · 1.05 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public int CountPS(String S, int N) {
int i =0;
int counter =0 ;
while(i<N-1){
int k = i+1 ;
while(k <= N){
if(S.substring(i,k).length() > 1){
if(isPallindrom(S.substring(i,k))){
++ counter;
}
}
else if(k == N){
if(S.substring(i).length() > 1){
if(isPallindrom(S.substring(i,k))){
++ counter;
}
}
}
k++;
}
i++;
}
return counter;
}
static boolean isPallindrom(String S){
int n = S.length();
int i =0;
while(i < n/2){
if(S.charAt(i) != S.charAt(n-i-1)){
return false;
}
i++;
}
return true;
}
}