6
\$\begingroup\$

I recently started learning Java, algorithms, and data structures. I am trying to check if a string is palindrome. I would like a review to see where and how I could improve.

import java.util.Stack;

class Palindromer {

Stack<Character> stack = new Stack<Character>();
int top = -1;

public boolean isPalindrome(String s) {
    for (int i = 0; i < s.length(); i++) {
        stack.push(s.charAt(i));
        top++;
    }

    System.out.println("value of top is " + top);
    String returnString = "";

    while (!stack.isEmpty()) {
        returnString += stack.pop();
    }
    System.out.println(returnString);

    return returnString.equals(s);
}

}

public class PalindromeChecker {
public static void main(String[] args) {
    Palindromer palin = new Palindromer();
    if (palin.isPalindrome("BananaB")) {
        System.out.println("is palindrome");
    } else {
        System.out.println("not a palindrome");
    }
}
}
\$\endgroup\$
  • \$\begingroup\$ Is using stack a requirement here? \$\endgroup\$ – Legato Sep 19 '15 at 14:46
  • \$\begingroup\$ no i used stack. I want to know if there are any other efficient ways to check whether a string is palindrome or not. \$\endgroup\$ – wandermonk Sep 19 '15 at 15:28
6
\$\begingroup\$

Extract routine logic into methods.

Your evaluation process should be its own method. This is something you'd want to repeat and it would make it easy to test multiple cases.

Your main method could just be:

public static void main(String[] args) {
    System.out.println(isPalindromic("racecar"));
}

Use JUnit tests

On the topic of tests, get into the habit of writing them, a good framework for testing is JUnit. You generally want to test something for edge cases and ensure that it works before sharing.

Example test case:

@Test 
public void validPalindromes() {
    assertTrue(isPalindromic("racecar"));
    assertTrue(isPalindromic("mom"));
    assertTrue(isPalindromic("avid diva"));
}

Semantic considerations of palindrome

One decision you should make is how you want to account for letter case, and punctuation. The sentence, "A car, a man, a maraca." is a palindrome by English standards, but whether it is or not will be up to your implementation.

A better way to check for palindrome.

What you want to do is just reverse a string and test it against the string, fortunately StringBuilder has a reverse method.

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

So to check if a word is a valid string, you can simply have:

public static boolean isPalindromic(String str) {
    return str.equals(reverse(str));
}

If you want to account for letter case you can use equalsIgnoreCase and for punctuation you should use a Regex.

Adding these factors in would give us:

public static boolean isPalindromic(String str) {
  String potentialPalindrome = str.replaceAll("[^a-zA-Z]", "");
  return potentialPalindrome.equalsIgnoreCase(reverse(potentialPalindrome));
}

Simplest implementation

If you don't want the overhead of using StringBuilder, you can always use a loop:

public static boolean isPalindromic(String s) {
    for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
        if (s.charAt(i) != s.charAt(j)) {
            return false;
        }
    }
    return true;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.