In this HackerRank Day 18: Queues and Stacks problem solution To solve this challenge, we must first take each character in s, enqueue it in a queue, and also push that same character onto a stack. Once that's done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means s isn't a palindrome).

Write the following declarations and implementations:

  1. Two instance variables: one for your stack, and one for your queue.
  2. A void pushCharacter(char ch) method that pushes a character onto a stack.
  3. A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
  4. A char popCharacter() method that pops and returns the character at the top of the stack instance variable.
  5. A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.

HackerRank Day 18: Queues and Stacks problem solution

Problem solution in Python programming.

class Solution:
    def __init__(self):
        self.word = []
        self.instance = []
    
    # A void pushCharacter(char ch) method that pushes a character onto a stack.
    def pushCharacter(self, char):
        self.word.append(char)
    
    # A void enqueueCharacter(char ch) method that enqueues a character in the instance variable.
    def enqueueCharacter(self, char):
        self.instance.insert(0,char)
    
    # A char popCharacter() method that pops and returns the character at the top of the instance variable.
    def popCharacter(self):
        return self.word.pop()
    
    # A char dequeueCharacter() method that dequeues and returns the first character in the instance variable.
    def dequeueCharacter(self):
        return self.instance.pop()



Problem solution in Java programming.

public class Solution {
    // Write your code here.
    LinkedList queue = new LinkedList();
    Stack<Character> stack = new Stack<Character>();
    
    void pushCharacter(char ch) {
        stack.push(ch);
    }
    
    void enqueueCharacter(char ch) {
        queue.add(ch);
    }
    
    char popCharacter() {
        return stack.pop();
    }
    
    char dequeueCharacter() {
        return (char)queue.removeFirst();
    }
    




Problem solution in C++ programming.

#include <iostream>
#include <queue>
#include <stack>


using namespace std;

class Solution {
    //Write your code here
    private:
    queue<char> myq;
    stack<char> mys;
    public:
    void pushCharacter(char ch1)
    {
        mys.push(ch1);
    }
    void enqueueCharacter(char ch2)  
    {
        myq.push(ch2);
    }  
    char popCharacter()
        {char x;
        while(!mys.empty())
          {  x =   mys.top();
           mys.pop();}
        return x;

    }
    char dequeueCharacter() 
        {char y;
        while(!myq.empty())
           { y =  myq.front();
            myq.pop();}
        return y;
    }
};


Problem solution in Java programming.

function Solution(){
    this.stack = [];
    this.queue = [];
    this.pushCharacter = function(char){
        this.stack.push(char);
    }
    this.enqueueCharacter = function(char){
        this.queue.unshift(char);
    }
    this.popCharacter = function(){
        return this.stack.pop();
    }
    this.dequeueCharacter = function(){
        return this.queue.pop();
    }
}