Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Day 18: Queues and Stacks in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Link for the Problem – Day 18: Queues and Stacks – Hacker Rank Solution
Day 18: Queues and Stacks– Hacker Rank Solution
Problem:
Welcome to Day 18! Today we’re learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video!
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, , 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:
- Two instance variables: one for your , and one for your .
- 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 instance variable.
- A char popCharacter() method that pops and returns the character at the top of the instance variable.
- A 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.
Day 18: Queues and Stacks – Hacker Rank Solution
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /** * @author Techno-RJ * */ public class Day18QueuesAndStacks { Stack<Character> st = new Stack<Character>(); Queue<Character> q = new LinkedList<Character>(); void pushCharacter(char ch) { st.push(ch); } void enqueueCharacter(char ch) { q.add(ch); } char popCharacter() { return st.pop(); } char dequeueCharacter() { return q.poll(); } }