Creating a recursive method for Palindrome

前端 未结 21 1933
野的像风
野的像风 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);
    
        }
    
    0 讨论(0)
  • 2020-12-03 06:45

    there's no code smaller than this:

    public static boolean palindrome(String x){
        return (x.charAt(0) == x.charAt(x.length()-1)) && 
            (x.length()<4 || palindrome(x.substring(1, x.length()-1)));
    }
    

    if you want to check something:

    public static boolean palindrome(String x){
        if(x==null || x.length()==0){
            throw new IllegalArgumentException("Not a valid string.");
        }
        return (x.charAt(0) == x.charAt(x.length()-1)) && 
            (x.length()<4 || palindrome(x.substring(1, x.length()-1)));
    }
    

    LOL B-]

    0 讨论(0)
  • 2020-12-03 06:45

    Palindrome example:

    static boolean isPalindrome(String sentence) {
    
        /*If the length of the string is 0 or 1(no more string to check), 
         *return true, as the base case. Then compare to see if the first 
         *and last letters are equal, by cutting off the first and last 
         *letters each time the function is recursively called.*/
    
        int length = sentence.length();
    
        if (length >= 1)
            return true;
        else {
            char first = Character.toLowerCase(sentence.charAt(0));
            char last = Character.toLowerCase(sentence.charAt(length-1));
    
            if (Character.isLetter(first) && Character.isLetter(last)) {
                if (first == last) {
                    String shorter = sentence.substring(1, length-1);
                    return isPalindrome(shorter);
                } else {
                    return false;
                }
            } else if (!Character.isLetter(last)) {
                String shorter = sentence.substring(0, length-1);
                return isPalindrome(shorter);
            } else {
                String shorter = sentence.substring(1);
                return isPalindrome(shorter);
            }
        }
    }
    

    Called by:

    System.out.println(r.isPalindrome("Madam, I'm Adam"));
    

    Will print true if palindrome, will print false if not.

    If the length of the string is 0 or 1(no more string to check), return true, as the base case. This base case will be referred to by function call right before this. Then compare to see if the first and last letters are equal, by cutting off the first and last letters each time the function is recursively called.

    0 讨论(0)
  • 2020-12-03 06:46

    I think, recursion isn't the best way to solve this problem, but one recursive way I see here is shown below:

    String str = prepareString(originalString); //make upper case, remove some characters 
    isPalindrome(str);
    
    public boolean isPalindrome(String str) {
       return str.length() == 1 || isPalindrome(str, 0);
    }
    
    private boolean isPalindrome(String str, int i) {
           if (i > str.length / 2) {
          return true;
       }
       if (!str.charAt(i).equals(str.charAt(str.length() - 1 - i))) {
          return false;
       }
       return isPalindrome(str, i+1);
    }
    
    0 讨论(0)
  • 2020-12-03 06:50
    public static boolean isPalindrome(String p)
        {
            if(p.length() == 0 || p.length() == 1)
                // if length =0 OR 1 then it is
                return true; 
    
             if(p.substring(0,1).equalsIgnoreCase(p.substring(p.length()-1))) 
                return isPalindrome(p.substring(1, p.length()-1));
    
    
            return false;
        }
    

    This solution is not case sensitive. Hence, for example, if you have the following word : "adinida", then you will get true if you do "Adninida" or "adninida" or "adinidA", which is what we want.

    I like @JigarJoshi answer, but the only problem with his approach is that it will give you false for words which contains caps.

    0 讨论(0)
  • 2020-12-03 06:50

    Simple Solution 2 Scenario --(Odd or Even length String)

    Base condition& Algo recursive(ch, i, j)

    1. i==j //even len

    2. if i< j recurve call (ch, i +1,j-1)

    3. else return ch[i] ==ch[j]// Extra base condition for old length

    public class HelloWorld {
    
    
     static boolean ispalindrome(char ch[], int i, int j) {
      if (i == j) return true;
    
      if (i < j) {
       if (ch[i] != ch[j])
        return false;
       else
        return ispalindrome(ch, i + 1, j - 1);
      }
      if (ch[i] != ch[j])
       return false;
      else
       return true;
    
     }
     public static void main(String[] args) {
      System.out.println(ispalindrome("jatin".toCharArray(), 0, 4));
      System.out.println(ispalindrome("nitin".toCharArray(), 0, 4));
      System.out.println(ispalindrome("jatinn".toCharArray(), 0, 5));
      System.out.println(ispalindrome("nittin".toCharArray(), 0, 5));
    
     }
    }
    
    0 讨论(0)
提交回复
热议问题