-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPalindromic_Substrings.java
49 lines (32 loc) · 1.13 KB
/
Palindromic_Substrings.java
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
49
public class Palindromic_Substrings {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "nitin";
System.out.println(countPalindromic(str));
}
public static int countPalindromic(String str) {
int count = 0;
// odd
for (int axis = 0; axis < str.length(); axis++) {
for (int orbit = 0; axis - orbit >= 0 && axis + orbit < str.length(); orbit++) {
if (str.charAt(axis - orbit) == str.charAt(axis + orbit)) {
count++;
} else {
break;
}
}
}
// even
for (double axis = 0.5; axis < str.length(); axis++) {
for (double orbit = 0.5; axis - orbit >= 0 && axis + orbit < str.length(); orbit++) {
if (str.charAt((int) (axis - orbit)) == str.charAt((int) (axis + orbit))) {
count++;
}
else {
break;
}
}
}
return count;
}
}