Skip to content

Instantly share code, notes, and snippets.

@alwaisy
Created November 26, 2022 01:51
Show Gist options
  • Save alwaisy/aa2c298da1774383c61acb4c0a1adfab to your computer and use it in GitHub Desktop.
Save alwaisy/aa2c298da1774383c61acb4c0a1adfab to your computer and use it in GitHub Desktop.
/**
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.
*/
// Solution all tests [0-6] were passed
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
class Solution {
constructor () {
this.stack = {}
this.queue = {}
this.head = 0
this.tail = 0
this.size = 0
}
pushCharacter(ch) {
this.size++
this.stack[this.size] = ch
this.ch = ch
}
enqueueCharacter(ch) {
this.queue[this.tail] = ch
this.tail++
}
popCharacter() {
let removed = this.stack[this.size]
delete this.stack[this.size]
this.size--
return removed
}
dequeueCharacter() {
let removed = this.queue[this.head]
delete this.queue[this.head]
this.head++
return removed
}
}
function main(){
// read the string s
var s=readLine();
var len=s.length;
// create the Solution class object p
var obj=new Solution();
//push/enqueue all the characters of string s to stack
for(var i=0;i<len;i++){
obj.pushCharacter(s.charAt(i));
obj.enqueueCharacter(s.charAt(i));
}
var isPalindrome=true;
/*
pop the top character from stack
dequeue the first character from queue
compare both the characters*/
for(var i=0;i<len/2;i++){
if(obj.popCharacter()!=obj.dequeueCharacter()){
isPalindrome=false;
break;
}
}
//finally print whether string s is palindrome or not
if(isPalindrome)
console.log("The word, "+s+", is a palindrome.");
else
console.log("The word, "+s+", is not a palindrome.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment