-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pallindrome_Stack.py
60 lines (48 loc) · 1020 Bytes
/
Pallindrome_Stack.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
# Python3 implementation of the approach
stack = []
top = -1
# push function
def push(ele: str):
global top
top += 1
stack[top] = ele
# pop function
def pop():
global top
ele = stack[top]
top -= 1
return ele
# Function that returns 1
# if str is a palindrome
def isPalindrome(string: str) -> bool:
global stack
length = len(string)
# Allocating the memory for the stack
stack = ['0'] * (length + 1)
# Finding the mid
mid = length // 2
i = 0
while i < mid:
push(string[i])
i += 1
# Checking if the length of the string
# is odd, if odd then neglect the
# middle character
if length % 2 != 0:
i += 1
# While not the end of the string
while i < length:
ele = pop()
# If the characters differ then the
# given string is not a palindrome
if ele != string[i]:
return False
i += 1
return True
# Driver Code
if __name__ == "__main__":
string = "madam"
if isPalindrome(string):
print("Yes")
else:
print("No")