We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a03b2ea commit 69f009eCopy full SHA for 69f009e
dynamic_programming/abbreviation.py
@@ -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