Katkat

Decode an anagram sentence using vocabulary constraints

UpstartSoftware Engineer · Technical Screen

Last updated

This question evaluates string manipulation, anagram detection, and constrained dictionary mapping skills, testing attention to character-level constraints and correct disambiguation among similar vocabulary entries.

You are given:

  • vocab: a list of lowercase words (the "dictionary")
  • s: a string containing space-separated scrambled words

Each scrambled word in s is guaranteed to be an anagram of exactly one word in vocab, with these extra constraints:

  • The original word and the scrambled word have the same length.
  • They share the same first character and the same last character.

Note: Some vocabulary words can be anagrams of each other (e.g., "pears" and "spear"). In that case, the additional constraint (same first/last character) is what makes the mapping unique for each scrambled token.

Return the decoded sentence by replacing each scrambled word with its matching vocabulary word, preserving word order and spaces.

Example:

  • vocab = ["pears", "spear", "something"]
  • s = "seapr sothinmeg"
  • Output: "spear something"

Implement a function:

  • Input: string[] vocab, string s
  • Output: string decoded