-
Notifications
You must be signed in to change notification settings - Fork 3
/
34_Quora_Make_Palindrome.py
executable file
·50 lines (35 loc) · 1.63 KB
/
34_Quora_Make_Palindrome.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
"""
Given a string, find the palindrome that can be made by inserting the fewest number of
characters as possible anywhere in the word. If there is more than one palindrome of
minimum length that can be made, return the lexicographically earliest one (the first one alphabetically).
For example, given the string "race", you should return "ecarace", since we
can add three letters to it (which is the smallest amount to make a palindrome).
There are seven other palindromes that can be made from "race" by adding three letters,
but "ecarace" comes first alphabetically.
As another example, given the string "google", you should return "elgoogle".
"""
def check_palindrome(string:str):
len_str = len(string)
# to check palindrome only need to
# check:
# 1- for even length string (len_str/2) the string
# 2- for odd length string (len_str//2)
str_end_idx = -1
for i in range(0, len_str//2):
# now check for matching char at corresponding ends of string
if string[i] != string[str_end_idx - i]:
return False
return True
def make_palindrome(string, add_chars="", list_end=-1):
if check_palindrome(add_chars+string):
return add_chars+string
# not palindrome so append chars to the start of the list to make
# palindrome
return make_palindrome(string, add_chars+string[list_end], list_end-1)
if __name__ == '__main__':
# print(check_palindrome('aba')) # True
# print(check_palindrome('race')) # False
# print(check_palindrome('abba')) # True
# print(make_palindrome('race'))
# print(make_palindrome('google'))
print(make_palindrome('quora'))