-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaliStr.java
More file actions
84 lines (64 loc) · 1.31 KB
/
PaliStr.java
File metadata and controls
84 lines (64 loc) · 1.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
//Pali longest String
import java.util.*;
public class PaliStr
{
static void substring(String s,int start,int end)
{
System.out.println(s.substring(start,end+1));
}
private static String palidrome(String s)
{
int i,n;
n=s.length();
boolean palidrome[][]=new boolean[n][n];
// check string at len 1
int maxlen=1;
for(i=0;i<n-1;i++)
{
palidrome[i][i]=true;
}
//check string for length 2
int start=0;
for(i=0;i<n-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
{
palidrome[i][i+1]=true;
start=i;
maxlen=2;
}
}
// check if string >2
int k,j;
for(k=3;k<=n;k++)
{
for(i=0;i<n-k+1;i++)
{
// make last index of string
j=i+k-1;
if(palidrome[i+1][j-1]&& s.charAt(i)==s.charAt(j))
{
palidrome[i][j]=true;
if(k>maxlen)
{
maxlen=k;
start=i;
}
}
}
}
System.out.println("Longest Palidrome!");
substring(s,start,start+maxlen-1);
// check length of longest palidrome
System.out.println("Length of longest palidrome!");
return String.valueOf(maxlen);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter the String U want to check Longest Palidrome");
s=sc.nextLine();
System.out.println(palidrome(s));
}
}