Hello coders, today we are going to solve Day 18: Queues and Stacks HackerRank Solution in C++, Java and Python.
Task
Welcome to Day 18! Today we’re learning about Stacks and Queues
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards. Can you determine if a given string, s, is a palindrome?
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:
- Two instance variables: one for your stack, and one for your queue.
- A void pushCharacter(char ch) method that pushes a character onto a stack.
- A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
- A char popCharacter() method that pops and returns the character at the top of the stack instance variable.
- A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.
Input Format
You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string s. It then calls the methods specified above to pass each character to your instance variables.
Constraints
- s is composed of lowercase English letters.
Output Format
You are not responsible for printing any output to stdout.
If your code is correctly written and s is a palindrome, the locked stub code will print The word, s, is a palindrome; otherwise, it will print The word, s, is not a palindrome.
Sample Input
racecar
Sample Output
The word, racecar, is a palindrome.
Solution – Day 18: Queues and Stacks
C++
#include <iostream> #include <stack> #include <queue> using namespace std; class Solution { //Write your code here private: stack<char> stk; queue<char> que; public: char c; void pushCharacter(char ch){ stk.push(ch); } char popCharacter(){ c=stk.top(); stk.pop(); return c; } void enqueueCharacter(char ch){ que.push(ch); } char dequeueCharacter(){ c=que.front(); que.pop(); return c; } }; int main() { // read the string s. string s; getline(cin, s); // create the Solution class object p. Solution obj; // push/enqueue all the characters of string s to stack. for (int i = 0; i < s.length(); i++) { obj.pushCharacter(s[i]); obj.enqueueCharacter(s[i]); } bool isPalindrome = true; // pop the top character from stack. // dequeue the first character from queue. // compare both the characters. for (int i = 0; i < s.length() / 2; i++) { if (obj.popCharacter() != obj.dequeueCharacter()) { isPalindrome = false; break; } } // finally print whether string s is palindrome or not. if (isPalindrome) { cout << "The word, " << s << ", is a palindrome."; } else { cout << "The word, " << s << ", is not a palindrome."; } return 0; }
Java
import java.io.*; import java.util.*; public class Solution { Queue<Character> qe=new LinkedList<>(); Stack<Character> st=new Stack<>(); void pushCharacter(char c){ st.push(c); } void enqueueCharacter(char c){ qe.add(c); } char popCharacter(){ return st.pop(); } char dequeueCharacter(){ return qe.remove(); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String input = scan.nextLine(); scan.close(); // Convert input String to an array of characters: char[] s = input.toCharArray(); // Create a Solution object: Solution p = new Solution(); // Enqueue/Push all chars to their respective data structures: for (char c : s) { p.pushCharacter(c); p.enqueueCharacter(c); } // Pop/Dequeue the chars at the head of both data structures and compare them: boolean isPalindrome = true; for (int i = 0; i < s.length/2; i++) { if (p.popCharacter() != p.dequeueCharacter()) { isPalindrome = false; break; } } //Finally, print whether string s is palindrome or not. System.out.println( "The word, " + input + ", is " + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) ); } }
Python
import sys class Solution: def __init__(self): self.stack = [] self.queue = [] def popCharacter(self): return self.stack.pop() def pushCharacter(self, char): self.stack.append(char) def dequeueCharacter(self): char = self.queue[0] self.queue = self.queue[1:] return char def enqueueCharacter(self, char): self.queue.append(char) # read the string s s=input() #Create the Solution class object obj=Solution() l=len(s) # push/enqueue all the characters of string s to stack for i in range(l): obj.pushCharacter(s[i]) obj.enqueueCharacter(s[i]) isPalindrome=True ''' pop the top character from stack dequeue the first character from queue compare both the characters ''' for i in range(l // 2): if obj.popCharacter()!=obj.dequeueCharacter(): isPalindrome=False break #finally print whether string s is palindrome or not. if isPalindrome: print("The word, "+s+", is a palindrome.") else: print("The word, "+s+", is not a palindrome.")
Disclaimer: The above Problem (Day 18: Queues and Stacks) is generated by Hacker Rank but the Solution in Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.