We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Abbreviation
Abbreviation
Sort by
recency
|
612 Discussions
|
Please Login in order to post a comment
My Java 8 solution, implementing a simple Dynamic Programming approach, passing all test-cases, for Max Score of 40. The code is fairly self-explanatory, especially with the comments.
C++ DP approach with idea in https://www.hackerrank.com/challenges/abbr/submissions/code/427897387
https://godbolt.org/z/Mx57Gxcca
def _remove_unused(a, b): _set_b=set(b) return "".join(list([x for x in a if x.isupper() or x.upper() in _set_b]))
def _build_dp(a): _dp = collections.defaultdict(int) for _, x in enumerate(a): _dp[x.upper()]+=1 return _dp
def _precheck_check_dp(dp_a, dp_b): for x in dp_b: if dp_a[x] < dp_b[x]: return False return True
def abbreviation(a, b): a=_remove_unused(a, b) if len(a) if not _precheck_check_dp(_build_dp(a),_build_dp(b)): return "NO" if _check(a, b): return "YES" else: return "NO"
My version. Last three tests is timeout. Think how to avoid recursion. Thanks to sxb427 idea: 1. daBcdc ABC 2. f(daBcdc, ABC) = f(daBcd, AB) or f(daBcd, ABC) 3. Improvment - preremoved lower symbols from which are not in b * aBcc ABC * f(aBc, AB) or f(daBc, ABC) => f(aBc, ABC) => True
Trying crazy things to solve it in O(NlogN) My code failed in 3 tests, but i can't think of an small test which it fails, can someone help?