Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Task1297 {
  4. public static void main(String[] args) {
  5.  
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. String input = scanner.nextLine();
  9. System.out.println(getLongestPalindrome(input));
  10. }
  11.  
  12. public static String getLongestPalindrome(String input) {
  13.  
  14. int maxLength = 0;
  15. String output = "";
  16.  
  17. for (int i = 0; i < input.length(); i++) {
  18. for (int j = i; j < input.length(); j++) {
  19. String current = input.substring(i, j + 1);
  20. if (maxLength < current.length() && isPalindrome(current)) {
  21. output = current;
  22. maxLength = current.length();
  23. }
  24. }
  25. }
  26. return output;
  27. }
  28.  
  29. public static boolean isPalindrome(String test) {
  30.  
  31. char[] array = test.toCharArray();
  32. if (array.length == 1) return true;
  33. boolean result = true;
  34.  
  35. for (int i = 0; i <= array.length / 2; i++) {
  36. if (array[i] != array[array.length - 1 - i]) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement