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.
classSolution:def__init__(self):self.stack=[]self.queue=[]defpushCharacter(self,char):self.stack.append(char)returnself.stackdefpopCharacter(self):returnself.stack.pop()defenqueueCharacter(self,char):self.queue.append(char)returnself.queuedefdequeueCharacter(self):returnself.stack.pop(0)# Write your code here# read the string ss=input()#Create the Solution class objectobj=Solution()l=len(s)# push/enqueue all the characters of string s to stackforiinrange(l):obj.pushCharacter(s[i])obj.enqueueCharacter(s[i])isPalindrome=True'''popthetopcharacterfromstackdequeuethefirstcharacterfromqueuecompareboththecharacters'''foriinrange(l// 2):ifobj.popCharacter()!=obj.dequeueCharacter():isPalindrome=Falsebreak#finally print whether string s is palindrome or not.ifisPalindrome: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 →
Python 3