Get the longest palindrome inside of a string - Java:
import java.util.Scanner;
public class LongestPalindrome {
/**
* This method get the longest palindrome inside of a string.
* @param inputString String to get the longest palindrome.
* @return String Obtains the longest palindrome inside of a string.
*/
public String getLongestPalindrome(String inputString) {
//Variable to store the longest palindrome
String longestPalindrome = "";
//Variable to store the current palindrome
String currentPalindrome = "";
//Variable to store the current palindrome length
int currentPalindromeLength = 0;
//Variable to store the longest palindrome length
int longestPalindromeLength = 0;
//Variable to store the current palindrome index
int currentPalindromeIndex = 0;
//Variable to store the longest palindrome index
int longestPalindromeIndex = 0;
//Variable to store the current palindrome start index
int currentPalindromeStartIndex = 0;
//Variable to store the current palindrome end index
int currentPalindromeEndIndex = 0;
//Variable to store the longest palindrome start index
int longestPalindromeStartIndex = 0;
//Variable to store the longest palindrome end index
int longestPalindromeEndIndex = 0;
//Loop through the string to get the longest palindrome
for (int i = 0; i < inputString.length(); i++) {
//Get the current palindrome start index
currentPalindromeStartIndex = i;
//Get the current palindrome end index
currentPalindromeEndIndex = i;
//Loop through the string to get the current palindrome length
while (currentPalindromeEndIndex < inputString.length() && inputString.charAt(currentPalindromeStartIndex) == inputString.charAt(currentPalindromeEndIndex)) {
//Increment the current palindrome end index by 1
currentPalindromeEndIndex++;
//Set the current palindrome length by 1 more than the previous one
currentPalindromeLength++;
}
//If the current palindrome length is greater than the previous one, set it as the new longest palindrome length and set its start and end index as well as its length
if (currentPalindromeLength > longestPalindromeLength) {
//Set the new longest palindrome length as the current one
longestPalindromeLength = currentPalindromeLength;
//Set the new longest palindrome start index as the current one's start index
longestPalindromeStartIndex = currentPalindromeStartIndex;
//Set the new longest palindrome end index as the current one's end index - 1 because we need to include it in our new longest palindrome length
longestPalindromeEndIndex = currentPalindromeEndIndex - 1;
}
//Reset the current palindrom length and its start and end index for next loop iteration
currentPalindromeLength = 0;
//Reset the current palindrom start index for next loop iteration
currentPalindromeStartIndex = i + 1;
}
//Get the longest palindrom from the string using its start and end index and its length
longestPalindrome = inputString.substring(longestPalindromeStartIndex, longestPalindromeEndIndex + 1);
//Return the longest palindrom from the string using its start and end index and its length
return longestPalindrome;
}
/**
* One Example
*/
public static void main(String args[]) throws IOException {
LongestPalindrome longestPalindrom= new LongestPalindrome ();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String inputString = scanner.nextLine();
System.out.println("The Longest Palindrom is: " + longestPalindrom.getLongestPalindrome(inputString));
}
}
Top comments (0)