Ctrl K

Key Phrase Cipher

Easy
Two friends exchange coded notes. They share a key phrase key_phrase that contains every lowercase English letter at least once (it may contain spaces as well). The substitution table is built like this: list the letters of the key in the order of their **first** appearance, skipping spaces and repeats. That gives 26 distinct letters. The first one decodes to a, the second to b, and so on up to z. Decode the note text: replace each letter using the table and leave spaces where they are. Return the resulting string.
Example 1
key_phrase='pack my box with five dozen liquor jugs', text='hmml pl fvvf' → 'meet at noon'

The key letters in order of first appearance are p a c k m y b o x w i t h f v e d z n l q u r j g s, so p→a, a→b, c→c, k→d, m→e, …, h→m, f→n, v→o, l→t. The note reads "meet at noon".

Example 2
key_phrase='zyxwvutsrqponmlkjihgfedcba', text='svool  dliow' → 'hello  world'

The key is the alphabet reversed, so every letter maps to its mirror; spaces stay as they are, double ones included.

Constraints
  • 26 ≤ len(key_phrase) ≤ 2000
  • 1 ≤ len(text) ≤ 2000
  • key_phrase and text consist of lowercase English letters and spaces
  • key_phrase contains every letter from a to z
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasySame Letter Cipher EasySame Letters, Reshuffled EasySame When Flipped EasyShuffle Into a Palindrome Easy
loading Python…
Input
key_phrase='pack my box with five dozen liquor jugs', text='hmml pl fvvf'
Expected
'meet at noon'

Run checks the first 3 tests in your browser. Submit checks all 199 tests on the server, hidden ones included.