-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram1.java
More file actions
71 lines (49 loc) · 1.3 KB
/
program1.java
File metadata and controls
71 lines (49 loc) · 1.3 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Input : str = "abaab"
Output: 3
Explanation :
All palindrome substring are :
"aba" , "aa" , "baab"
Input : str = "abbaeae"
Output: 4
Explanation :
All palindrome substring are :
"bb" , "abba" ,"aea","eae"
*/
package LeetCode_String;
import java.util.ArrayList;
public class program1 {
static int noPalindrome(String str) {
String s = "";
ArrayList<String> st = new ArrayList<String>();
for (int i = 0; i <= str.length(); i++) {
for (int j = i + 2; j <= str.length(); j++) {
st.add(str.substring(i, j));
}
}
System.out.print(st + " ");
int cnt = 0;
for (int i = 0; i < st.size(); i++) {
String substr = st.get(i);
int left = 0;
int right = substr.length() - 1;
boolean flag = true;
while (left < right) {
if (substr.charAt(left) != substr.charAt(right)) {
flag = false;
break;
}
left++;
right--;
}
if (flag) {
cnt++;
}
}
return cnt;
}
public static void main(String[] args) {
String str = "abbaeae";
System.out.println(noPalindrome(str));
}
}