Skip to content

Commit edfd6af

Browse files
authored
Merge pull request #812 from Manasi2001/issue-810
Palindrome Checker
2 parents 33c75bd + c81f6a5 commit edfd6af

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Aim: Determine whether the entered string is palindrome or not.
3+
4+
'''
5+
6+
class Solution:
7+
def __init__(self):
8+
self.stack = []
9+
self.queue = []
10+
return(None)
11+
12+
def pushCharacter(self, char):
13+
self.stack.append(char)
14+
15+
def popCharacter(self):
16+
return(self.stack.pop(-1))
17+
18+
def enqueueCharacter(self, char):
19+
self.queue.append(char)
20+
21+
def dequeueCharacter(self):
22+
return(self.queue.pop(0))
23+
24+
# read the string s
25+
s = input()
26+
27+
# creating the Solution class object
28+
obj = Solution()
29+
30+
l = len(s)
31+
32+
# push/enqueue all the characters of string s to stack
33+
for i in range(l):
34+
obj.pushCharacter(s[i])
35+
obj.enqueueCharacter(s[i])
36+
37+
isPalindrome = True
38+
'''
39+
pop the top character from stack
40+
dequeue the first character from queue
41+
compare both the characters
42+
'''
43+
for i in range(l // 2):
44+
if obj.popCharacter()!=obj.dequeueCharacter():
45+
isPalindrome = False
46+
break
47+
48+
# finally print whether string s is palindrome or not
49+
if isPalindrome:
50+
print("The word, "+s+", is a palindrome.")
51+
else:
52+
print("The word, "+s+", is not a palindrome.")
53+
54+
'''
55+
Sample Test Case:
56+
Input:
57+
level
58+
Output:
59+
The word, level, is a palindrome.
60+
Explaination:
61+
All the characters popped from stack, matched the ones dequeued from queue.
62+
63+
'''

0 commit comments

Comments
 (0)