-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiggestPalindromeString.java
63 lines (53 loc) · 1.72 KB
/
BiggestPalindromeString.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
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.company;
import java.util.LinkedList;
import java.util.TreeMap;
public class BiggestPalindromeString {
public static void main(String[] args) {
System.out.println("Hello world!");
// Input: Given string :"forgeeksskeegfor",
// Output: "geeksskeeg"
// Input: Given string :"Geeks",
// Output: "ee"
possiblesub("forgeeksskeegfo");
}
public static void possiblesub(String input) {
try {
LinkedList<String> possiblity = new LinkedList<>();
for (int i = 0; i <= input.length(); i++) {
for (int j = i + 1; j <= input.length(); j++) {
possiblity.add(input.substring(i, j));
}
}
TreeMap<Integer, String> ans = new TreeMap<>();
for (String strings : possiblity) {
if (reverse(strings)) {
ans.put(strings.length(), strings);
}
}
System.out.println("final outcome " + ans.descendingMap());
} catch (Exception e) {
System.out.println("The failure reason" + e);
}
}
public static boolean reverse(String in) {
LinkedList<Character> ch = new LinkedList<>();
int count = 0;
for (int i = (in.length() - 1); i >= 0; i--) {
ch.add(in.charAt(i));
}
LinkedList<Character> reverse = new LinkedList<>();
for (int a = 0; a < in.length(); a++) {
reverse.add(in.charAt(a));
if (in.charAt(a) == ch.get(a)) {
} else {
count = 1;
break;
}
}
if (count == 0) {
return true;
} else {
return false;
}
}
}