-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPalindromInString.java
More file actions
49 lines (45 loc) · 1.33 KB
/
FindPalindromInString.java
File metadata and controls
49 lines (45 loc) · 1.33 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
/*
@author Muhammed Fatih ÖZDİL
@since 07/03/2021
*/
package algorithmExamples;
public class FindPalindromInString {
static void printSubStr(String str1, int l, int h) {
System.out.println(str1.substring(l, h + 1));
}
static int longPalSubstr(String str1) {
int n = str1.length();
boolean table[][] = new boolean[n][n];
int mLength = 1;
for (int i = 0; i < n; ++i)
table[i][i] = true;
int strt = 0;
for (int i = 0; i < n - 1; ++i) {
if (str1.charAt(i) == str1.charAt(i + 1)) {
table[i][i + 1] = true;
strt = i;
mLength = 2;
}
}
for (int k = 3; k <= n; ++k) {
for (int i = 0; i < n - k + 1; ++i) {
int j = i + k - 1;
if (table[i + 1][j - 1] && str1.charAt(i) == str1.charAt(j)) {
table[i][j] = true;
if (k > mLength) {
strt = i;
mLength = k;
}
}
}
}
System.out.print("The longest palindrome substring in the given string is; ");
printSubStr(str1, strt, strt + mLength - 1);
return mLength;
}
public static void main(String[] args) {
String str1 = "thequickbrownfoxxofnworbquickthe";
System.out.println("The given string is: " + str1);
System.out.println("The length of the palindromic substring is: " + longPalSubstr(str1));
}
}