forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_palindromic_substring.java
42 lines (41 loc) · 1.33 KB
/
Longest_palindromic_substring.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
import java.util.*;
class Scratch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
System.out.println(longestPalindrome(str));
}
public static String longestPalindrome(String s) {
int stringLength = s.length();
int[][] memoise = new int[stringLength][stringLength];
char[] stringArray = s.toCharArray();
int maxStartIndex = 0;
int maxEndIndex = 0;
int i=0;
for(int k=0; k<stringLength; k++)
{
for(int j=0; j<stringLength-k; j++){
i=j+k;
if(stringArray[i] == stringArray[j]){
if(k <= 2){
memoise[i][j] = 1;
maxStartIndex = j;
maxEndIndex = i;
}
else if (memoise[i-1][j+1] == 1){
memoise[i][j] = 1;
maxStartIndex = j;
maxEndIndex = i;
}
else
memoise[i][j] = 0;
}
else{
memoise[i][j] = 0;
}
}
}
return s.substring(maxStartIndex, maxEndIndex+1);
}
}