Java Method Recursion check palindrome

Description

Java Method Recursion check palindrome

public class Main{
   public static void main(String[] args) {
      System.out.println(palindrome("mom"));
      System.out.println(palindrome("level"));
      System.out.println(palindrome("demo"));
      System.out.println(palindrome("hiih"));
   }/* w ww . j ava2  s .co m*/
   private static boolean palindrome(String str) {
      if (str.length() <= 1) {
         return true;
      }
      String first = str.substring(0, 1);
      String last = str.substring(str.length() - 1);
      return first.equals(last)
            && palindrome(str.substring(1, str.length() - 1));
   }
}



PreviousNext

Related