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.
Python3 solution using a candidate solution method. Starting from a root candidate "" (empty string), iterate over the letters of a. At each letter, iterate over the candidates, taking the following actions to form the next set of candidates:
1. propagate candidate, i.e. do not add current letter and retain candidate (string) as-is.
2. fork candidate, i.e. add current letter to create a new candidate.
3. terminate candidate, e.g. when the letter must be added (b/c it is uppercase) but adding the letter makes the candidate invalid.
defabbreviation(a,b):candidates={""}#candidatesols(a.k.a."dp")forletina:new_candidates={""}forcincandidates:trial_c=c+let.upper()ifb.startswith(trial_c):# if l upper: must update c -> trial_c# if l lower: keep c and add trial_cnew_candidates.add(trial_c)#(fork)iflet.islower():new_candidates.add(c)#(propagate)eliflet.islower():new_candidates.add(c)#(propagate)else:continue#(terminate)candidates=new_candidatesifbincandidates:return"YES"else:return"NO"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Abbreviation
You are viewing a single comment's thread. Return to all comments →
Python3 solution using a candidate solution method. Starting from a root candidate "" (empty string), iterate over the letters of a. At each letter, iterate over the candidates, taking the following actions to form the next set of candidates: 1. propagate candidate, i.e. do not add current letter and retain candidate (string) as-is. 2. fork candidate, i.e. add current letter to create a new candidate. 3. terminate candidate, e.g. when the letter must be added (b/c it is uppercase) but adding the letter makes the candidate invalid.