# Palindrome Checker in python using deque

[17088 views]

Have you ever heard of the Palindrome problem in python that is a classic problem in programming? It’s really an amazing problem to solve. In this article, you will learn to create a palindrome checker in python using a deque data structure.

If a string or a number is read from both sides and still remains unaltered then it said to be a palindrome. Some of the examples of palindrome include radar, tit, peep, 212, 404, etc. To make a palindrome checker in python, you should first understand Deque data structure

#### What is Deque data structure?

Deque is also known as a double-ended queue It is similar to queue in which collection of items are ordered properly. It consists of two ends front and rear. You can add new items on any side you wish. Deque data structure is unique in the sense that it contains no restriction in adding or removing items. Queues and stacks are sometimes considered as the deque’s specializations.

#### Creating Palindrome Checker

There are many ways to make a palindrome checker. You can do so by creating an algorithm of inputting strings of characters and checking whether they are a palindrome or not. The solution to the problem will begin by using a deque.

There are many ways to make a palindrome checker. You can do so by creating an algorithm of inputting strings of characters and checking whether they are a palindrome or not. The solution to the problem will begin by using a deque.

As you know, we can remove the characters at both sides simultaneously, compare them and continue to do so unless the characters will differ. After a few comparisons, you will be left with a deque containing one character. However, it depends on the string whether it is odd or even. It will check that the string is a palindrome or not. For the complete solution, refer to the following code:

from collections import deque def palchecker(string): chardeque = deque() for ch in string: chardeque.addRear(ch) stillEqual = True while chardeque.size() > 1 and stillEqual: first = chardeque.removeFront() last = chardeque.removeRear() if first != last: stillEqual = False return stillEqual print(palchecker("lsdkjfskf")) print(palchecker("radar"))

#### Output

false true

Isn't it's easy to code a palindrome checker in python using collections module and deque data structure? Now you can easily check any string or number if it is a palindrome or not.