Problem : Write a HackerRank Solution For Day 18 : Queues and Stacks or Hacker Rank Solution Program In Python and java For ” Day 18 : Scope ” or Hacker Rank 30 days of code in Python and Java Solution: Day 18 : Queues and Stacks or Hackerrank solution for 30 Days of Code Challenges or Hacker rank 30 days of code in Python and Java Solution, Day 18 : Queues and Stacks , or Python and java Logic & Problem Solving: Day 1 : Queues and Stacks .

Question from HackerRank :

Welcome to Day 18! Today we’re learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video!

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, , is a palindrome?

To solve this challenge, we must first take each character in , 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  isn’t a palindrome).

Write the following declarations and implementations:

  1. Two instance variables: one for your , and one for your .
  2. void pushCharacter(char ch) method that pushes a character onto a stack.
  3. void enqueueCharacter(char ch) method that enqueues a character in the  instance variable.
  4. char popCharacter() method that pops and returns the character at the top of the  instance variable.
  5. char dequeueCharacter() method that dequeues and returns the first character in the  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 . It then calls the methods specified above to pass each character to your instance variables.

Constraints

  •  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  is a palindrome, the locked stub code will print ; otherwise, it will print 

Sample Input

racecar

Sample Output

The word, racecar, is a palindrome.

Solution Of the Question is :

More Hacker Rank Solutions : Hacker Rank solutions 30 day challenge

Get latest FREE projects : IT , Engineering projects for FREE

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)

Day 18 Hacker Rank Solution : Queues and Stacks

Full code will look like this :

import sys
import os

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.")

Code in Java

Queues and Stacks or Hacker Rank Solution Program in java For ” Day 18 : Scope ” or Hacker Rank 30 days of code Java Solution: Day 18 : Queues and Stacks or Hackerrank solution for 30 Days of Code Challenges or Hacker rank 30 days of code Java Solution .

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

    char dequeueCharacter()
        {
        return queue.remove();
    }

Thanks for the visit checkout the day 19 tutorial as well.. 

🙂

Leave a Reply

Your email address will not be published.