Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Task1297 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- System.out.println(getLongestPalindrome(input));
- }
- public static String getLongestPalindrome(String input) {
- int maxLength = 0;
- String output = "";
- for (int i = 0; i < input.length(); i++) {
- for (int j = i; j < input.length(); j++) {
- String current = input.substring(i, j + 1);
- if (maxLength < current.length() && isPalindrome(current)) {
- output = current;
- maxLength = current.length();
- }
- }
- }
- return output;
- }
- public static boolean isPalindrome(String test) {
- char[] array = test.toCharArray();
- if (array.length == 1) return true;
- boolean result = true;
- for (int i = 0; i <= array.length / 2; i++) {
- if (array[i] != array[array.length - 1 - i]) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement