-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge2.java
57 lines (47 loc) · 1.1 KB
/
Challenge2.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
import java.util.Scanner;
class Challenge{
static String longestPalSubstr(String s) {
int n = s.length();
int start = 0, end = 1;
int low, hi;
for (int i = 0; i < n; i++) {
low = i - 1;
hi = i;
// 0 1 2 3 4
// g e e k s
while (low >= 0 && hi < n && s.charAt(low) == s.charAt(hi)) {
if (hi - low + 1 > end) {
start = low;
end = hi - low + 1;
}
low--;
hi++;
}
low = i - 1;
hi = i + 1;
// 0 1 2 3 4s
// g e s k s
while (low >= 0 && hi < n && s.charAt(low) == s.charAt(hi)) {
if (hi - low + 1 > end) {
start = low;
end = hi - low + 1;
}
low--;
hi++;
}
}
return s.substring(start, start+end);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter no of test cases");
int t = input.nextInt();
while (t-- >0) {
System.out.println("Enter the string: ");
String string =input.next();
// String s = "forgeeksskeegfor";
//String s = "geeks";
System.out.println("Longest palindrome substring is:"+longestPalSubstr(string));
}
}
}