Skip to content

Commit 69f009e

Browse files
author
Erdenezul
committed
add abbrevation solution to dp
1 parent a03b2ea commit 69f009e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

dynamic_programming/abbreviation.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
https://www.hackerrank.com/challenges/abbr/problem
3+
You can perform the following operation on some string, :
4+
5+
1. Capitalize zero or more of 's lowercase letters at some index i
6+
(i.e., make them uppercase).
7+
2. Delete all of the remaining lowercase letters in .
8+
9+
Example:
10+
a=daBcd and b="ABC"
11+
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
12+
"""
13+
def abbr(a, b):
14+
n = len(a)
15+
m = len(b)
16+
dp = [[False for _ in range(m + 1)] for _ in range(n + 1)]
17+
dp[0][0] = True
18+
for i in range(n):
19+
for j in range(m + 1):
20+
if dp[i][j]:
21+
if j < m and a[i].upper() == b[j]:
22+
dp[i + 1][j + 1] = True
23+
if a[i].islower():
24+
dp[i + 1][j] = True
25+
return dp[n][m]
26+
27+
28+
if __name__ == "__main__":
29+
print abbr("daBcd", "ABC") # expect True

0 commit comments

Comments
 (0)