-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel45.py
More file actions
38 lines (29 loc) · 901 Bytes
/
level45.py
File metadata and controls
38 lines (29 loc) · 901 Bytes
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
# Given a string, find the shortest possible string which can be achieved by adding
# characters to the end of initial string to make it a palindrome.
# def solution(st):
# stack = []
# idx = 0
# if st[:]==st[::-1]:
# return st
# for j in range(len(st)):
# if st[j] in stack:
# stack.pop(0)
# elif st[j] not in stack:
# stack.append(st[j])
# print(f'stack {stack} itr {j}')
# return stack
# print(solution("aaaabaa")) # baa
def solution(st):
stack = []
idx = 0
if st[:]==st[::-1]:
return st
for i in range(len(st)):
if st[i] not in stack:
stack.append(st[i])
idx = st.index(st[i])
print(f'idx {idx} {st[i]}')
print(stack)
print(st[idx-1::-1])
return st+st[idx-1::-1]
print(solution("aaaaba")) # aaaaba - aaa