Given a string str, the task is to find whether the given string is a palindrome or not using stack.
Input: str = “geeksforgeeks”
Input: str = “madam”
- 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 it with the current character i.e. string[i].
- If there is 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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a given string is Even-Odd Palindrome or not
- C Program to Check if a Given String is Palindrome
- Check if a string is palindrome in C using pointers
- Check if it is possible to create a palindrome string from given N
- Check given string is oddly palindrome or not | Set 2
- Check given string is oddly palindrome or not
- Check if a given string is a rotation of a palindrome
- Check if any anagram of a string is palindrome or not
- Python program to check if a string is palindrome or not
- Java program to check whether a string is a Palindrome
- Check if there exists any sub-sequence in a string which is not palindrome
- Recursive function to check if a string is palindrome
- Given two strings check which string makes a palindrome first
- TCP Client-Server Program to Check if a Given String is Palindrome
- Check if a string can be rearranged to form special palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if string is palindrome after removing all consecutive duplicates
- Python program to check if given string is vowel Palindrome
- Check if string remains palindrome after removing given number of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.