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 dequeuing, 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:
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 a palindrome.
Sample Input
racecar
Sample Output
The word, racecar, is a palindrome.
Submit Your Solution Here: Click Here
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 a palindrome.
Sample Input
racecar
Sample Output
The word, racecar, is a palindrome.
Submit Your Solution Here: Click Here
Day 18 Queues And Stacks Hackerrank Solution in C++
#include <iostream>
using namespace std;
class Solution
{
string queue = "", stack = "";
public:
void pushCharacter(char c)
{
this->stack = c + this->stack;
};
void enqueueCharacter(char c)
{
this->queue += c;
};
char popCharacter()
{
char c = this->stack[0];
this->stack = this->stack.substr(1);
return c;
};
char dequeueCharacter()
{
char c = this->queue[0];
this->queue = this->queue.substr(1);
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;
}
Queues And Stacks Explanation
Before going to solve first we have to know how Stack and Queue work and what is the difference between Stack and Queue so let's take an into of both Stack and Queue. here we have to check string is Palindrome or Not. First, see this C++ Program To Check String Is Palindrome Or Not and also See this C++ Program To Find Whether A Number Is Palindrome Or Not.
Stack: Stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element of the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out).
Queue: A Queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure
Stack: Stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element of the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out).
Queue: A Queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure
0 Comments: