Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Palindrome Check using Queue
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a string s, determine if that string is a palindrome using a queue data structure. Return true if the string is a palindrome. Otherwise, return false.

A palindrome is a word, number, phrase, or other sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.

Examples

Example 1

  • Input: s = "madam"
  • Output: true
  • Explanation: The word "madam" reads the same forwards and backwards.

Example 2

  • Input: s = "openai"
  • Output: false
  • Explanation: The word "openai" does not read the same forwards and backwards.

Example 3

  • Input: s = "A man a plan a canal Panama"
  • Output: true
  • Explanation: The phrase "A man a plan a canal Panama" reads the same forwards and backwards when we ignore spaces and capitalization.

Solution

To solve this problem, the string is first preprocessed by removing all non-alphanumeric characters and converting it to lowercase to ensure consistency. Then, the characters of the cleaned string are stored in a deque (implemented as an array). The algorithm checks if the string is a palindrome by repeatedly removing and comparing characters from both the front and back of the deque. If all pairs of characters match as they are removed, the string is confirmed to be a palindrome; otherwise, it is not. This approach efficiently utilizes the deque's properties to verify the palindrome nature of the string.

Step-by-Step Algorithm

  1. Normalize the String: Remove any spaces, punctuation, and convert all characters to the same case (lowercase or uppercase) to ensure consistency in comparison.

  2. Initialize a Dequeue: Create an empty Dequeue that will be used to store characters of the string.

  3. Enqueue Characters: Iterate over the normalized string and enqueue each character into the Dequeue.

  4. Check for Palindrome:

    • Dequeue a character from the front and end of the queue.
    • Compare these two dequeued characters.
    • If at any point the two characters do not match, return false, indicating the string is not a palindrome.
    • Repeat step-4, until there are less than 2 characters left in the queue.
  5. End of Queue: If you've iterated through the queue without finding a mismatch, return true, indicating the string is a palindrome.

Algorithm Walkthrough

mediaLink

1 of 6

Code

Here is how we can implement this algorithm:

Python3
Python3

. . . .

Complexity Analysis

Time Complexity

  • Preprocessing (removing non-alphanumeric characters and converting to lowercase): The algorithm processes the input string once to remove non-alphanumeric characters and convert it to lowercase. This takes O(N) time, where N is the length of the input string.

  • Deque operations:

    • Deque insertion: Each character from the preprocessed string is added to the deque in constant time, O(1). This results in a total of O(N) for adding all characters to the deque.
    • Deque comparison: In the main loop, the algorithm compares characters from both ends of the deque. For each comparison, two characters are removed from the deque. This loop runs approximately N/2 times, resulting in O(N) operations.

Overall time complexity: O(N), where N is the length of the input string.

Space Complexity

  • Deque space: The deque stores all characters from the preprocessed string, requiring O(N) space.

  • Additional space: The string that results from preprocessing (removing non-alphanumeric characters and converting to lowercase) also requires O(N) space.

Overall space complexity: O(N).

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible