Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#215 from erdenezul/dp_abbreviation
Browse files Browse the repository at this point in the history
add abbrevation solution to dp
  • Loading branch information
harshildarji authored Dec 13, 2017
2 parents ac2b2d0 + 69f009e commit 2dcbbd4
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions dynamic_programming/abbreviation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
https://www.hackerrank.com/challenges/abbr/problem
You can perform the following operation on some string, :
1. Capitalize zero or more of 's lowercase letters at some index i
(i.e., make them uppercase).
2. Delete all of the remaining lowercase letters in .
Example:
a=daBcd and b="ABC"
daBcd -> capitalize a and c(dABCd) -> remove d (ABC)
"""
def abbr(a, b):
n = len(a)
m = len(b)
dp = [[False for _ in range(m + 1)] for _ in range(n + 1)]
dp[0][0] = True
for i in range(n):
for j in range(m + 1):
if dp[i][j]:
if j < m and a[i].upper() == b[j]:
dp[i + 1][j + 1] = True
if a[i].islower():
dp[i + 1][j] = True
return dp[n][m]


if __name__ == "__main__":
print abbr("daBcd", "ABC") # expect True

0 comments on commit 2dcbbd4

Please sign in to comment.