Skip to content

Instantly share code, notes, and snippets.

@Kirill255
Last active June 12, 2019 08:55
Show Gist options
  • Save Kirill255/da40e849ca1244aae5e709b494746756 to your computer and use it in GitHub Desktop.
Save Kirill255/da40e849ca1244aae5e709b494746756 to your computer and use it in GitHub Desktop.
class Helper_Queues_and_Stacks {
#stack = [];
#queue = [];
pushCharacter(char) {
this.#stack.push(char);
}
enqueueCharacter(char) {
this.#queue.push(char);
}
popCharacter() {
return this.#stack.pop();
}
dequeueCharacter() {
return this.#queue.shift();
}
}
const isPalindrome = (word) => {
let is_Palindrome = true;
const len = word.length;
const obj = new Helper_Queues_and_Stacks();
// push/enqueue all the characters of string s to stack
for (let i = 0; i < len; i++) {
obj.pushCharacter(word.charAt(i));
obj.enqueueCharacter(word.charAt(i));
}
// pop the top character from stack dequeue the first character from queue compare both the characters
for (let i = 0; i < len / 2; i++) {
if (obj.popCharacter() !== obj.dequeueCharacter()) {
is_Palindrome = false;
break;
}
}
if (is_Palindrome) {
console.log("The word, " + word + ", is a palindrome.");
} else {
console.log("The word, " + word + ", is not a palindrome.");
}
};
isPalindrome("racecar");
isPalindrome("yes");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment