Grokking Data Structures & Algorithms for Coding Interviews
0% completed
Solution: Palindrome Check using Queue
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
# The initial soluton did is incorrect. A queue should only remove elements from either the front or the end but not both. function isPalindrome(s: string): boolean { // Normalize the string: convert to lowercase and remove non-alphanumeric characters const normalizedStr = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); // Initialize a queue (array used as a queue) and a stack const queue: string[] = []; const stack: string[] = []; // Add each character of the normalized string to both the queue and the stack for (const char of normalizedStr) { queue.push(char); stack.push(char); } // Check if the string is a palindrome using the queue and the stack while (queue.length > 0) { if (queue.shift() !== stack.pop())
A
Anonymous
· 3 years ago
I realized after taking this that there are different built in library or premade library to make deque easier, maybe it good to explain the library as well
Reading Progress
0%