Check whether the given string is Palindrome using Stack
Given string str, the task is to find whether the given string is a palindrome or not using a stack.
Examples:
Input: str = “geeksforgeeks”
Output: No
Input: str = “madam”
Output: Yes
Approach:
- Find the length of the string say len. Now, find the mid as mid = len / 2.
- Push all the elements till mid into the stack i.e. str[0…mid-1].
- If the length of the string is odd then neglect the middle character.
- Till the end of the string, keep popping elements from the stack and compare them with the current character i.e. string[i].
- If there is a mismatch then the string is not a palindrome. If all the elements match then the string is a palindrome.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string s)
{
int length = s.size();
stack< char > st;
int i, mid = length / 2;
for (i = 0; i < mid; i++) {
st.push(s[i]);
}
if (length % 2 != 0) {
i++;
}
char ele;
while (s[i] != '\0' )
{
ele = st.top();
st.pop();
if (ele != s[i])
return false ;
i++;
}
return true ;
}
int main()
{
string s = "madam" ;
if (isPalindrome(s)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
C
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * stack;
int top = -1;
void push( char ele)
{
stack[++top] = ele;
}
char pop()
{
return stack[top--];
}
int isPalindrome( char str[])
{
int length = strlen (str);
stack = ( char *) malloc (length * sizeof ( char ));
int i, mid = length / 2;
for (i = 0; i < mid; i++) {
push(str[i]);
}
if (length % 2 != 0) {
i++;
}
while (str[i] != '\0' ) {
char ele = pop();
if (ele != str[i])
return 0;
i++;
}
return 1;
}
int main()
{
char str[] = "madam" ;
if (isPalindrome(str)) {
printf ( "Yes" );
}
else {
printf ( "No" );
}
return 0;
}
|
Java
class GFG
{
static char []stack;
static int top = - 1 ;
static void push( char ele)
{
stack[++top] = ele;
}
static char pop()
{
return stack[top--];
}
static int isPalindrome( char str[])
{
int length = str.length;
stack = new char [length * 4 ];
int i, mid = length / 2 ;
for (i = 0 ; i < mid; i++)
{
push(str[i]);
}
if (length % 2 != 0 )
{
i++;
}
while (i < length)
{
char ele = pop();
if (ele != str[i])
return 0 ;
i++;
}
return 1 ;
}
public static void main(String[] args)
{
char str[] = "madam" .toCharArray();
if (isPalindrome(str) == 1 )
{
System.out.printf( "Yes" );
}
else
{
System.out.printf( "No" );
}
}
}
|
Python3
stack = []
top = - 1
def push(ele: str ):
global top
top + = 1
stack[top] = ele
def pop():
global top
ele = stack[top]
top - = 1
return ele
def isPalindrome(string: str ) - > bool :
global stack
length = len (string)
stack = [ '0' ] * (length + 1 )
mid = length / / 2
i = 0
while i < mid:
push(string[i])
i + = 1
if length % 2 ! = 0 :
i + = 1
while i < length:
ele = pop()
if ele ! = string[i]:
return False
i + = 1
return True
if __name__ = = "__main__" :
string = "madam"
if isPalindrome(string):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static char []stack;
static int top = -1;
static void push( char ele)
{
stack[++top] = ele;
}
static char pop()
{
return stack[top--];
}
static int isPalindrome( char []str)
{
int length = str.Length;
stack = new char [length * 4];
int i, mid = length / 2;
for (i = 0; i < mid; i++)
{
push(str[i]);
}
if (length % 2 != 0)
{
i++;
}
while (i < length)
{
char ele = pop();
if (ele != str[i])
return 0;
i++;
}
return 1;
}
public static void Main(String[] args)
{
char []str = "madam" .ToCharArray();
if (isPalindrome(str) == 1)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isPalindrome(s)
{
var length = s.length;
var st = [];
var i, mid = parseInt(length / 2);
for (i = 0; i < mid; i++) {
st.push(s[i]);
}
if (length % 2 != 0) {
i++;
}
var ele;
while (i != s.length)
{
ele = st[st.length-1];
st.pop();
if (ele != s[i])
return false ;
i++;
}
return true ;
}
var s = "madam" ;
if (isPalindrome(s)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(N).
Last Updated :
13 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...