Skip to content

Commit

Permalink
Merge pull request #1541 from haseebzaki-07/new_branch_14
Browse files Browse the repository at this point in the history
Check palindrome using deue
  • Loading branch information
ajay-dhangar authored Oct 28, 2024
2 parents 90a6fa6 + 2e45d87 commit e303071
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions docs/Queue/check-palindrome-using-dequeue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
id: check-palindrome-using-deque
title: "Check Palindrome Using Deque"
sidebar_label: "Palindrome Check with Deque"
sidebar_position: 9
description: "A guide to checking if a string is a palindrome using a deque data structure for efficient operations."
tags: [data structure, deque, palindrome]
---

# Check Palindrome Using Deque

A palindrome is a string that reads the same forwards and backwards. This guide explains how to check if a string is a palindrome using a **deque** (double-ended queue) data structure, which allows for efficient operations at both ends.

## Introduction

Using a deque to check for palindromes leverages its ability to add or remove elements from both ends in constant time. This makes it a suitable choice for the task, as we can compare characters from the front and back of the string until we reach the center.

![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTwWr4bJaQGsjcIJzTxYf_KG-rUl7g8lpQWlw&s)

## Steps to Check Palindrome

1. **Add** each character of the string to the deque.
2. **Remove and compare** characters from both ends of the deque until the middle is reached.
3. If all pairs match, the string is a palindrome; otherwise, it is not.

## Implementation

### Python Code

```cpp

#include <iostream>
#include <deque>
#include <string>
using namespace std;

bool isPalindrome(const string& s) {
deque<char> d;

// Load the deque with each character from the string
for (char ch : s) {
d.push_back(ch);
}

// Compare characters from both ends
while (d.size() > 1) {
if (d.front() != d.back()) {
return false; // Not a palindrome if characters don't match
}
d.pop_front(); // Remove the front character
d.pop_back(); // Remove the back character
}

return true; // All pairs matched, so it is a palindrome
}

int main() {
string str1 = "radar";
cout << str1 << " is a palindrome: " << (isPalindrome(str1) ? "True" : "False") << endl;

string str2 = "hello";
cout << str2 << " is a palindrome: " << (isPalindrome(str2) ? "True" : "False") << endl;

return 0;
}
```
## Complexity
### Time Complexity:
is_palindrome:
𝑂(𝑛)
O(n), where 𝑛 is the length of the string (since each character is dequeued once from both ends).
### Space Complexity: 𝑂(𝑛)
O(n), where 𝑛 is the number of characters in the deque.
## Explanation of Code
Initialize a deque with the characters of the string.
Use a while loop to repeatedly remove characters from the front and back of the deque.
Return False if any character pair doesn't match; otherwise, return True when the loop completes.
## Applications
The palindrome check has applications in string processing, data validation, and computer science algorithms. Deques provide an efficient way to check for palindromes with minimal memory overhead.

0 comments on commit e303071

Please sign in to comment.