-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_8_BF.java
More file actions
64 lines (51 loc) · 2.23 KB
/
Problem_8_BF.java
File metadata and controls
64 lines (51 loc) · 2.23 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
package strings;
import java.util.*;
//* Problem Title => Write a program to find the longest Palindrome in a string. [ The Longest palindromic Substring ]
public class Problem_8_BF {
// Function to print a substring
static void printSubString(String inputString, int low, int high) {
for (int i = low; i <= high; ++i) {
System.out.print(inputString.charAt(i));
}
}
// This function prints the longest palindrome substring in a string.
// It also returns the length of the longest palindrome
static int findLongestPalindromicSubString(String inputString) {
// Get length of the input string
int stringLength = inputString.length();
// All substrings of length 1 are palindromes
int maxLength = 1;
int startIndex = 0;
// Nested loop to find starting and ending indexes of the longest palindrome
for (int i = 0; i < stringLength; i++) {
for (int j = i; j < stringLength; j++) {
boolean isPalindrome = true;
// Check if the substring is a palindrome
for (int k = 0; k < (j - i + 1) / 2; k++) {
if (inputString.charAt(i + k) != inputString.charAt(j - k)) {
isPalindrome = false;
break;
}
}
// If a palindrome is found and its length is greater than the current maximum
if (isPalindrome && (j - i + 1) > maxLength) {
startIndex = i;
maxLength = j - i + 1;
}
}
}
System.out.println("Longest palindrome substring is: ");
printSubString(inputString, startIndex, startIndex + maxLength - 1);
// Return length of the longest palindrome substring
return maxLength;
}
// Driver Code
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the input string: ");
String inputString = scanner.nextLine();
System.out.println(
"Length of the longest palindrome substring is: " + findLongestPalindromicSubString(inputString));
scanner.close();
}
}