Double Substitution Decryption
Updated
Problem statement
A message was transformed by two one-to-one character substitutions:
- Alice's characters were substituted into Bob's characters.
- Bob's characters were substituted into Carol's characters.
You receive aligned clear/cipher training text for each stage:
alicePlainandbobCipherdefine the Alice-to-Bob mapping.bobPlainandcarolCipherdefine the Bob-to-Carol mapping.
When learning either mapping, remove all whitespace from both aligned strings before pairing their characters. The non-whitespace strings have equal lengths and consistently define one-to-one mappings.
Decrypt encryptedMessage from Carol's alphabet back to Bob's alphabet, then from Bob's alphabet back to Alice's alphabet. Preserve every whitespace character in encryptedMessage unchanged. Return the fully decrypted message.
Function
decryptDoubleSubstitution(alicePlain: String, bobCipher: String, bobPlain: String, carolCipher: String, encryptedMessage: String) → StringExamples
Example 1
alicePlain = "abc"bobCipher = "bcd"bobPlain = "bcd"carolCipher = "cde"encryptedMessage = "edc cde"return = "cba abc"Reversing the Carol-to-Bob mapping changes edc cde to dcb bcd. Reversing the Bob-to-Alice mapping then produces cba abc.
Example 2
alicePlain = "a b c"bobCipher = "x y z"bobPlain = "x y z"carolCipher = "m n o"encryptedMessage = "mno onm"return = "abc cba"Whitespace in the training text does not create mapping entries. The single space in the encrypted message is preserved.
Constraints
- Each input string has length from
1through10^5. - After whitespace removal, each training pair has equal length.
- Each training pair defines a consistent one-to-one mapping.
- Every non-whitespace character needed while decrypting is covered by the corresponding inverse mapping.
- Characters are case-sensitive.
More Upstart Online Assessment questions
- Buffet Day GainsOA · Seen Sep 2026
- Case-Insensitive Anagram CheckOA · Seen Sep 2026
- Filter Duplicates While Preserving OrderOA · Seen Sep 2026
- Scale Ingredient QuantitiesOA · Seen Sep 2026
- ABO Parent Genotype CombinationsOA · Seen Aug 2026
- Chunked Byte Stream ChecksumsOA · Seen Aug 2026
- Cluster Size on DiskOA · Seen Aug 2026
- Add Drama to Text GroupsOA · Seen Jul 2026
- Lexicographic Word FrequenciesOA · Seen Jul 2026
- Minimum Absolute Pair DifferenceOA · Seen Jul 2026
Similar questions at other companies
- Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Perfect SubstringAXQ Capital · OA
- Count Suffix Word PairsTikTok · OATikTok interview questions
- Password Clues and the Next CommandOpenAI · OAOpenAI interview questions
- Decode Words with Anagram KeysCresta · OACresta interview questions
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Maximum Valid Substring FrequencyMicrosoft · OAMicrosoft interview questions
- Longest Distinct StringSuperhuman · OASuperhuman interview questions
- Longest String ChainVisa · OAVisa interview questions