Input Format
You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string . It then calls the methods specified above to pass each character to your instance variables.
Constraints
- s is composed of lowercase English letters.
Output Format
You are not responsible for printing any output to stdout.
If your code is correctly written and s is a palindrome, the locked stub code will print (The word s is a palindroms) otherwise, it will print (The word is not a palindroms)
Sample Input
racecar
Sample Output
The word, racecar, is a palindrome.
Solution:
import sys
class Solution:
# Write your code here
stack=[]
queue=[]
f=-1
r=-1
i=0
def pushCharacter(self,c):
self.stack.append(c)
def enqueueCharacter(self,c):
if(len(self.queue)==0):
self.f+=1
self.r+=1
self.queue.append(c)
else:
self.r+=1
self.queue.append(c)
def popCharacter(self):
return(self.stack.pop())
def dequeueCharacter(self):
if(self.f!=0):
self.i+=1
return(self.queue.pop(self.f-self.i))
self.f+=1
else:
return(self.queue.pop(self.f))
self.f+=1
# read the string s
s=input()
#Create the Solution class object
obj=Solution()
l=len(s)
# push/enqueue all the characters of string s to stack
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
#finally print whether string s is palindrome or not.
if isPalindrome:
print("The word, "+s+", is a palindrome.")
else:
print("The word, "+s+", is not a palindrome.")
0 Comments