We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
for i in range(l):
obj.pushCharacter(s[i])
obj.enqueueCharacter(s[i])
isPalindrome=True
'''
pop the top character from stack
dequeue the first character from queue
compare both the characters
'''
for i in range(l // 2):
if obj.popCharacter()!=obj.dequeueCharacter():
isPalindrome=False
break
if isPalindrome:
print("The word, "+s+", is a palindrome.")
else:
print("The word, "+s+", is not a palindrome.")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 18: Queues and Stacks
You are viewing a single comment's thread. Return to all comments →
In Python 3
import sys
class Solution:
s=input()
obj=Solution()
l=len(s)
for i in range(l): obj.pushCharacter(s[i]) obj.enqueueCharacter(s[i])
isPalindrome=True ''' pop the top character from stack dequeue the first character from queue compare both the characters ''' for i in range(l // 2): if obj.popCharacter()!=obj.dequeueCharacter(): isPalindrome=False break
if isPalindrome: print("The word, "+s+", is a palindrome.") else: print("The word, "+s+", is not a palindrome.")