Conservation researchers at a botanical reserve protect their field notes about rare plant locations with a simple substitution cipher keyed off a passphrase chosen fresh each day. To build the day's cipher, scan the passphrase from left to right and record each lowercase letter the first time it is seen, skipping spaces and skipping any letter that has already been recorded; this produces the 26 lowercase letters in some order, one for every letter of the alphabet. The letter recorded at position i (0-indexed) in that scan stands, in any encoded note written under this passphrase, for the i-th letter of the alphabet ('a' for position 0, 'b' for position 1, and so on). A space in an encoded note always decodes to a space, and every other character decodes by this substitution.
Given today's passphrase and one encoded field note, print the decoded note.
Line 1: the passphrase — a string of lowercase English letters and spaces, containing every one of the 26 letters at least once (letters may repeat; only each letter's first appearance is used). Line 2: the encoded field note — a string of lowercase English letters and spaces.
Print the decoded note on a single line, preserving its exact spacing.
Example 1
Input
bcdefghijklmnopqrstuvwxyza uijt jt b tfdsfu
Expected
this is a secret
Explanation
Scanning the passphrase left to right gives the distinct order b,c,d,...,z,a, so position 0 maps to 'b'->'a', position 1 to 'c'->'b', and so on, wrapping so 'a' (recorded last, position 25) maps to 'z'->'a'. Effectively every letter in the note decodes to the letter one earlier in the alphabet, with 'a' wrapping from 'z'. Applying that to "uijt jt b tfdsfu" letter by letter gives "this is a secret".
Example 2
Input
the quick brown fox jumps over the lazy dog suuqs stiu kupu
Expected
seeds safe here
Explanation
Scanning the passphrase left to right and keeping only each letter's first appearance gives the order t,h,e,q,u,i,c,k,b,r,o,w,n,f,x,j,m,p,s,v,l,a,z,y,d,g, which position-by-position maps to a,b,c,...,z. Looking up each letter of "suuqs stiu kupu" in that table (s->s, u->e, u->e, q->d, s->s, then s->s, t->a, i->f, u->e, then k->h, u->e, p->r, u->e) spells out "seeds safe here", with the spaces staying spaces.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →