forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#215 from erdenezul/dp_abbreviation
add abbrevation solution to dp
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |