Skip to content

Commit 252acc8

Browse files
committed
Added #121
1 parent 5323480 commit 252acc8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

challenges/make-palindrome.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
#121
3+
Google
4+
5+
Given a string which we can delete at most k, return whether you can make a palindrome.
6+
7+
For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get 'waterretaw'.
8+
"""
9+
10+
def palindromable(word, k):
11+
start = 0
12+
end = len(word) - 1
13+
14+
return palindromableHelper(word, k, start, end)
15+
16+
def palindromableHelper(word, k, x, y):
17+
if k < 0 :
18+
return False
19+
20+
while x < y:
21+
if word[x] != word[y]:
22+
return palindromableHelper(word, k-1, x+1, y) or palindromableHelper(word, k-1, x, y-1)
23+
x += 1
24+
y -= 1
25+
26+
return True
27+
28+
def main():
29+
print(palindromable("waterrfetawx", 2))
30+
print(palindromable("abcdefg", 1))
31+
print(palindromable("madam",0))
32+
33+
if __name__ == "__main__":
34+
main()

index.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* [#43 Max Stack Implementation](./challenges/max-stack-implementation.py)
3434
* [#55 URL Shortener](./challenges/url-shortener.py)
3535
* [#89 Valid Binary Search Tree](./challenges/valid-binary-search-tree.py)
36+
* [#121 Make Palidrome](./challenges/make-palindrome.py)
3637
* [#157 Is Permutation Palindrome](./challenges/is-permutation-palindrome.py)
3738
* [#171 Busiest Time In Building](./challenges/busiest-time-in-building.py)
3839
* [#173 Flatten Nested Dictionary](./challenges/flatten-nested-dictionary.py)

0 commit comments

Comments
 (0)