import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
// Function to check if a given string is a palindrome
private static boolean isPalindrome(String s) {
int start = 0;
int end = s.length() - 1;
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
// Function to find the length of the longest palindromic substring
public static int longestPalindromicSubstring(String A) {
int n = A.length();
if (n == 0) return 0;
HashSet<String> substrings = new HashSet<>();
int maxLength = 1;
// Generate all possible substrings
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
substrings.add(A.substring(i, j));
}
}
// Iterate over all substrings to find the longest palindromic substring
for (String sub : substrings) {
if (isPalindrome(sub)) {
int len = sub.length();
if (len > maxLength) {
maxLength = len;
}
}
}
return maxLength;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.print("Enter a string: ");
String A = scn.nextLine(); // Correct method to read a string input
int maxLength = longestPalindromicSubstring(A);
System.out.println("The length of the longest palindromic substring is: " + maxLength);
scn.close(); // Close the scanner
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: