Check string for palindrome

360,942

Solution 1

Why not just:

public static boolean istPalindrom(char[] word){
    int i1 = 0;
    int i2 = word.length - 1;
    while (i2 > i1) {
        if (word[i1] != word[i2]) {
            return false;
        }
        ++i1;
        --i2;
    }
    return true;
}

Example:

Input is "andna".
i1 will be 0 and i2 will be 4.

First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).
So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).
Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.

Solution 2

You can check if a string is a palindrome by comparing it to the reverse of itself:

public static boolean isPalindrome(String str) {
    return str.equals(new StringBuilder(str).reverse().toString());
}

or for versions of Java earlier than 1.5,

public static boolean isPalindrome(String str) {
    return str.equals(new StringBuffer().append(str).reverse().toString());
}

EDIT: @FernandoPelliccioni provided a very thorough analysis of the efficiency (or lack thereof) of this solution, both in terms of time and space. If you're interested in the computational complexity of this and other possible solutions to this question, please read it!

Solution 3

A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:

boolean isPalindrome(String str) {    
    int n = str.length();
    for( int i = 0; i < n/2; i++ )
        if (str.charAt(i) != str.charAt(n-i-1)) return false;
    return true;    
}

Solution 4

Alternatively, recursion.

For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:

private boolean isPalindrome(String s) {
    int length = s.length();

    if (length < 2) // If the string only has 1 char or is empty
        return true;
    else {
        // Check opposite ends of the string for equality
        if (s.charAt(0) != s.charAt(length - 1))
            return false;
        // Function call for string with the two ends snipped off
        else
            return isPalindrome(s.substring(1, length - 1));
    }
}

OR even shorter, if you'd like:

private boolean isPalindrome(String s) {
    int length = s.length();
    if (length < 2) return true;
    return s.charAt(0) != s.charAt(length - 1) ? false :
            isPalindrome(s.substring(1, length - 1));
}

Solution 5

Go, Java:

public boolean isPalindrome (String word) {
    String myWord = word.replaceAll("\\s+","");
    String reverse = new StringBuffer(myWord).reverse().toString();
    return reverse.equalsIgnoreCase(myWord);
}

isPalindrome("Never Odd or Even"); // True
isPalindrome("Never Odd or Even1"); // False
Share:
360,942
DarkLeafyGreen
Author by

DarkLeafyGreen

Tackling Complexity in the Heart of Software

Updated on December 05, 2021

Comments

  • DarkLeafyGreen
    DarkLeafyGreen over 2 years

    A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.

    To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work. However I want to know if it is right or if there is something to improve.

    Here is my code:

    public class Aufg1 {
        public static void main(String[] args) {
            String wort = "reliefpfpfeiller";
            char[] warray = wort.toCharArray(); 
            System.out.println(istPalindrom(warray));       
        }
    
        public static boolean istPalindrom(char[] wort){
            boolean palindrom = false;
            if(wort.length%2 == 0){
                for(int i = 0; i < wort.length/2-1; i++){
                    if(wort[i] != wort[wort.length-i-1]){
                        return false;
                    }else{
                        palindrom = true;
                    }
                }
            }else{
                for(int i = 0; i < (wort.length-1)/2-1; i++){
                    if(wort[i] != wort[wort.length-i-1]){
                        return false;
                    }else{
                        palindrom = true;
                    }
                }
            }
            return palindrom;
        }
    }