hqt

Palindrome string recursive

hqt
Jan 29th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. public class StringPalindrome {
  2.     public static void main(String[] args) {
  3.         recursiveSolve("ddabccba");
  4.     }
  5.  
  6.     public static void recursiveSolve(String input) {
  7.         int result = recursiveSolve(input, 0, input.length() - 1);
  8.         System.out.println(result);
  9.     }
  10.  
  11.     public static int recursiveSolve(String input, int start, int end) {
  12.         if (start == end) return 0;
  13.         if (start > end) return Integer.MAX_VALUE;
  14.  
  15.         if (start == end - 1) {
  16.             if (input.charAt(start) == input.charAt(end)) {
  17.                 return 0;
  18.             } else {
  19.                 return 1;
  20.             }
  21.         }
  22.  
  23.         if (input.charAt(start) == input.charAt(end)) {
  24.             return recursiveSolve(input, start + 1, end - 1);
  25.         }
  26.  
  27.         return Math.min(recursiveSolve(input, start + 1, end), recursiveSolve(input, start, end - 1)) + 1;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment