Creating a recursive method for Palindrome

前端 未结 21 1938
野的像风
野的像风 2020-12-03 06:28

I am trying to create a Palindrome program using recursion within Java but I am stuck, this is what I have so far:

 public static void main (String[] args){
         


        
21条回答
  •  情深已故
    2020-12-03 06:43

    /**
         * Function to check a String is palindrome or not
         * @param s input String
         * @return true if Palindrome
         */
        public boolean checkPalindrome(String s) {
    
            if (s.length() == 1 || s.isEmpty())
                return true;
    
            boolean palindrome = checkPalindrome(s.substring(1, s.length() - 1));
    
            return palindrome && s.charAt(0) == s.charAt(s.length() - 1);
    
        }
    

提交回复
热议问题