1

I wrote a palindrome using stacks in java. But, due to some reason while popping the stack the program is not working as expected. I have also tried using the top and iterate using this variable. Either ways it is not working.

import java.util.Stack;

class Palindromer {

Stack<Character> stack = null;
int top = -1;

public boolean isPalindrome(String s) {
    for (int i = 0; i < s.length(); i++) {
        stack = new Stack<Character>();
        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");
    }
}
}
wandermonk
  • 6,856
  • 6
  • 43
  • 93
  • Don't instantiate a new, empty `Stack` in your `for` loop... Btw: [related](http://stackoverflow.com/questions/7569335/reverse-a-string-in-java). – Kenney Sep 19 '15 at 13:23
  • wow thanks that works. – wandermonk Sep 19 '15 at 13:24
  • @WhiteViking no duplicate, the question your refer has very low quality. – Wolf Sep 19 '15 at 13:27
  • @Wolf I referred to it because it poses the same question and has a correct answer, but you are right, the question itself was of poor quality. I deleted my comment. – WhiteViking Sep 19 '15 at 13:33
  • 1
    I did not find the question very useful. Also, I wanted to know if there is any efficient way to check the string palindrome with less time complexity. – wandermonk Sep 19 '15 at 13:35
  • 1
    @WhiteViking Well, there is an [answer](http://stackoverflow.com/a/17872271/2932052) that does it quite good ;-) – Wolf Sep 19 '15 at 13:40

3 Answers3

1

You should place the new Stack<Character>(); outside the loop.

The way you do it:

    for (int i = 0; i < s.length(); i++) {
        stack = new Stack<Character>();
        stack.push(s.charAt(i));
        top++;
    }

the stack variable is reassigned each loop and does contain only one character after the loop. Change it into

    stack = new Stack<Character>();
    for (int i = 0; i < s.length(); i++) {
        stack.push(s.charAt(i));
        top++;
    }

BTW: Better declare stack and top in the isPalindrome method. So you get rid of the error in top with further calls.

Wolf
  • 9,679
  • 7
  • 62
  • 108
0

You should initialise the stack outside the loop i.e.

for (int i = 0; i < s.length(); i++) {
        stack = new Stack<Character>();
        stack.push(s.charAt(i));
        top++;
    }

should be

stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {

        stack.push(s.charAt(i));
        top++;
    }
sol4me
  • 15,233
  • 5
  • 34
  • 34
0

Use the below version of isPalindrome method, Stack object was getting instantiated in each iteration of loop and hence you were not getting the expected behavior.

public boolean isPalindrome(String s) {
    stack = new Stack<Character>(); 
    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);
}
M4ver1k
  • 1,505
  • 1
  • 15
  • 26