There's a little-known secret in Banjo-Kazooie that you can interleave the three Cheato codes on the Treasure Trove Cove Sandcastle floor and it will still recognize all of them. So you could, for instance, enter
GOLDFEATHERS all at once.
Your task is to take a list of cheat codes and a code entry and determine which of the codes are activated.
A code entry is valid if and only if some substring of the entry can spell out a subset of the cheat codes by assigning letters one at a time to exactly one word in order until all codes are exactly filled in. For example, applying this to the above code entry:
BLUREEGOLDDFFEEGEAATHTHEEGSRSRS BLU_E________E_G_________GS____ ___R_E___D_F__E__A_TH__E___RS__ ______GOL_D_F___E_A__TH_E____RS
- Both cheats and entries will contain only uppercase letters A-Z.
- Each code in the input list will appear only once.
- If two cheat codes share a prefix or a suffix, their shared letters must still appear twice to be counted, thus
REDGOLDFEATHERSwould, for the purposes of this challenge, only return
- Each code entry can only contain one matching substring.
- If it is ambigous how a code-matching substring could be chosen, choose the one that starts the earliest and is the longest match for that particular starting point.
- The order of matched codes returned does not matter.
- If it is ambiguous which code could be used, prefer cheat codes that appear earlier in the code list.
- A substring does not match if some of the letters inside it cannot be assigned to a cheat code. For instance
BLUEXEGGSdoes not match any cheat codes.
- If no codes are matched, you may return an empty list or any falsy value.
Note that this may not necessarily be consistent with the nuances of the game`s implementation
Example Test Cases
Codes: BLUEEGGS Entry: BLUEEGGS Result: BLUEEGGS
Codes: BLUEEGGS REDFEATHERS GOLDFEATHERS Entry: BANJOGOLDBLUREEDFEATHEERGGSSKAZOOIE Result: BLUEEGGS REDFEATHERS
Codes: BLUEEGGS REDFEATHERS GOLDFEATHERS Entry: KAZOOIEBLUREEGOLDDFFEEGEAATHTHEEGSRSRSBANJO Result: BLUEEGGS REDFEATHERS GOLDFEATHERS
Codes: BLUEEGGS REDFEATHERS GOLDFEATHERS Entry: BANJOKAZOOIE Result: <falsy>
Codes: CAT DOG Entry: CCADOGATT Result: DOG (The substring may not contain the same code twice)
Codes: COG ATE CAT DOG Entry: CDCOAGTE Result: DOG CAT (Match the earliest and longest substring)
Codes: ACE BDF ABC DEF Entry: ABCDEF Result: ACE BDF (Prefer matching codes earlier in the list)
Standard procedure for code golf applies. I/O can use any convenient format. Fewest bytes wins.