2017-11-21 07:28:55 +00:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
"""
|
2018-01-21 07:25:19 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2017-11-21 07:28:55 +00:00
|
|
|
def abbr(a, b):
|
2019-10-09 19:20:19 +00:00
|
|
|
"""
|
|
|
|
>>> abbr("daBcd", "ABC")
|
|
|
|
True
|
|
|
|
>>> abbr("dBcd", "ABC")
|
|
|
|
False
|
|
|
|
"""
|
2017-11-21 07:28:55 +00:00
|
|
|
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__":
|
2019-10-09 19:20:19 +00:00
|
|
|
# print(abbr("daBcd", "ABC")) # expect True
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|