Python Program to Check String is Palindrome using Stack

When it is required to check if a string is a palindrome using stack data structure, a stack class is created, and push and pop methods are defined to add and delete values from stack. Another method checks to see if the stack is empty or not.

A palindrome is a word that reads the same forwards and backwards, like "madam" or "racecar". Using a stack's LIFO (Last In, First Out) property, we can reverse a string and compare it with the original.

Stack Implementation

First, let's create a stack class with basic operations ?

class Stack_structure:
    def __init__(self):
        self.items = []

    def check_empty(self):
        return self.items == []

    def push_val(self, data):
        self.items.append(data)

    def pop_val(self):
        return self.items.pop()

# Test the stack
stack = Stack_structure()
stack.push_val('A')
stack.push_val('B')
print("Popped:", stack.pop_val())
print("Is empty:", stack.check_empty())
Popped: B
Is empty: False

Palindrome Check Algorithm

The algorithm pushes each character onto the stack, then pops them to create a reversed string ?

class Stack_structure:
    def __init__(self):
        self.items = []

    def check_empty(self):
        return self.items == []

    def push_val(self, data):
        self.items.append(data)

    def pop_val(self):
        return self.items.pop()

def check_palindrome(text):
    my_instance = Stack_structure()
    
    # Push all characters onto stack
    for character in text.lower():
        my_instance.push_val(character)
    
    # Pop characters to create reversed string
    reversed_text = ''
    while not my_instance.check_empty():
        reversed_text = reversed_text + my_instance.pop_val()
    
    # Compare original with reversed
    return text.lower() == reversed_text

# Test with different strings
test_strings = ["MalayalaM", "racecar", "hello", "madam"]

for string in test_strings:
    if check_palindrome(string):
        print(f"'{string}' is a palindrome")
    else:
        print(f"'{string}' is not a palindrome")
'MalayalaM' is a palindrome
'racecar' is a palindrome
'hello' is not a palindrome
'madam' is a palindrome

How It Works

  1. Push Phase: Each character is pushed onto the stack
  2. Pop Phase: Characters are popped (in reverse order) to build reversed string
  3. Comparison: Original and reversed strings are compared
  4. Result: If they match, it's a palindrome

Interactive Example

Here's the complete program with user input ?

class Stack_structure:
    def __init__(self):
        self.items = []

    def check_empty(self):
        return self.items == []

    def push_val(self, data):
        self.items.append(data)

    def pop_val(self):
        return self.items.pop()

my_instance = Stack_structure()
text_input = input('Enter the string... ')

# Convert to lowercase for case-insensitive comparison
text_lower = text_input.lower()

for character in text_lower:
    my_instance.push_val(character)

reversed_text = ''
while not my_instance.check_empty():
    reversed_text = reversed_text + my_instance.pop_val()

if text_lower == reversed_text:
    print(f"'{text_input}' is a palindrome")
else:
    print(f"'{text_input}' is not a palindrome")

Key Points

  • Stack's LIFO property naturally reverses the string
  • Case-insensitive comparison makes it more practical
  • Time complexity: O(n) for both push and pop operations
  • Space complexity: O(n) for storing characters in stack

Conclusion

Using a stack to check palindromes demonstrates the LIFO principle effectively. The stack reverses the string naturally, making palindrome detection straightforward through simple string comparison.

Updated on: 2026-03-25T18:39:40+05:30

982 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements