-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestPalindromicSubstring.java
58 lines (49 loc) · 1.73 KB
/
LongestPalindromicSubstring.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
public class LongestPalindromicSubstring {
public static boolean isPalindrome(String str){
int left=0;
int right= str.length()-1;
while(left<right){
if(str.charAt(left) != str.charAt(right) ) return false;
left++;
right--;
}
return true;
}
public String longestPalindrome(String s) {
int maxLength = 0;
String answer = "";
if(s.length() == 1){
return s;
}
boolean beforePalindrom = isPalindrome(s);
if(beforePalindrom){
return s;
}
for (int i = 0; i <= s.length() ; i++) {
int j = s.length();
while(i<j) {
String substring = s.substring(i, j);
boolean isPalindrome = isPalindrome(substring);
if (isPalindrome) {
int count = substring.length();
if(count > maxLength){
answer = substring;
maxLength = count;
}
}
j--;
}
}
return answer;
}
public static void main(String[] args) {
LongestPalindromicSubstring object = new LongestPalindromicSubstring();
Scanner scanner = new Scanner(System.in); // Create a Scanner object for user input
System.out.print("Enter a string: ");
String inputString = scanner.nextLine(); // Read the user's input
String result = object.longestPalindrome(inputString); // Find the longest palindromic substring
System.out.println("Longest palindromic substring: " + result);
scanner.close(); // Close the Scanner to release resources
}
}