Using Stack, how can I check if one string is the inverse of another?

Asked

Viewed 496 times

-1

EX: x = "ABABBA" and y= ABBABA

I’ve done the class Pilha, with the methods, but I am doubtful how to implement.

package questao02;
public class Pilha<T> {
    T[]arrayelem;
    int topo;
    int index = 0;
    public Pilha(int max){
        this.arrayelem = (T[]) new Object[max];
        this.topo = -1;
    }
    public boolean estaCheia(){
      return this.topo == this.arrayelem.length-1;
    }
    public boolean estaVazia(){
        return this.topo == -1;
    }
    public boolean inserir(T elem){
        if(!this.estaCheia()){
            this.arrayelem[++ this.topo] = elem;
            return true;
        }
        return false;
    }
    public T remover(){
        if(!this.estaVazia()){
           T aux = this.arrayelem[this.topo];
            this.topo--;
            return aux;
        }
        return null;
    }
}

I wanted to understand the logic, whether it’s a slump or not.

  • 1

    You can’t understand what you want to do, but the code is necessary. Check the help center for items in the section asking: https://answall.com/help

  • I’ve already modified the code

  • Palindrome means that the string backwards is equal to the string itself. But in your case, x and y are not palindromes - in fact one is the inverse of the other. You want to check if they are palindromes or if one is inverse of the other?

  • I want to check if one is the reverse of the other... I thought they were Palindromos,but they’re not,

  • If it’s an exercise and you’re forced to use stack, it’s simple: Pop the x characters, then go pop and compare to the y characters. But if you don’t have to stack, you can reverse x and compare with y.

  • I will have to use stack... Then in case,I would stack everything before,inside a loop,and then I would pop check. That would be it?

  • Basically this :-)

Show 2 more comments

1 answer

1


First, this is not palindrome. Palindrome is a string that reading from beginning to end is equal to the end to start. Example (ignoring accents and spaces):

Help me get on the bus in Morocco

That being said, to check if y is the inverse of x you must invert x and check if it is equal to y. To do this using a stack, you put the x characters on the stack. Then assemble a string by removing the characters from the stack. As in a stack the last character that entered is the first to exit, the resulting string is the inverse of the first. So just compare with y.

Pilha<Character> pilha = new Pilha<>(100);
String x = "ABABBA";
String y = "ABBABA";
String z = "ABBABB";

for (int i = 0; i < x.length(); i++) {
    Character c = x.charAt(i);
    pilha.inserir(c);
}

String reversed = new String();
int i = 0;
while (!pilha.estaVazia()) {
    reversed += pilha.remover();
}

System.out.println("x é inverso de y? " + reversed.equals(y));
System.out.println("x é inverso de z? " + reversed.equals(z));
  • Very serious work. I was already desperate kk.

  • Then accept the answer =) https://answall.com/help/someone-answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.