-
Notifications
You must be signed in to change notification settings - Fork 183
/
19 - Day 18 - Queues and Stacks.py
69 lines (53 loc) · 1.44 KB
/
19 - Day 18 - Queues and Stacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/30-queues-stacks/problem
# Difficulty: Easy
# Max Score: 30
# Language: Python
# ========================
# Solution
# ========================
import sys # Solution made without using the required library
class Solution:
# Write your code here
def __init__(self):
self.top = 0
self.rear = 0
self.a = list()
self.r = list()
def pushCharacter(self, char):
self.a.append(char)
self.top += 1
def enqueueCharacter(self, char):
self.r.append(char)
self.rear += 1
def popCharacter(self):
self.top -= 1
return self.a[self.top]
def dequeueCharacter(self):
return self.r[i]
# 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.")