Count Dictionary Originals Sharing an Encryption

Quick Overview

Count Dictionary Originals Sharing an Encryption. Implement Encrypter using distinct lowercase keys, each mapped to a two-character string; different keys may map to the same string. encrypt(word1) concatenates the mapped pairs, returning empty if any character lacks a mapping. decrypt(word2) returns the number of distinct dictionary words whose encryption equals word2. Keep the original constructor, encrypt, and integer-returning decrypt API.

Count Dictionary Originals Sharing an Encryption

Company: Duolingo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Implement an `Encrypter` object from distinct lowercase `keys`, their corresponding two-character `values`, and a dictionary of permitted original words. Different keys may share the same pair. `encrypt(word1)` replaces each character by its mapped pair and concatenates the pairs, returning an empty string if a character has no mapping. `decrypt(word2)` considers its consecutive two-character blocks: each block may be replaced by any key mapped to that pair. Return the number of distinct resulting words that occur in the dictionary. Preserve the constructor and both methods. ```cpp class Encrypter { public: Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary); string encrypt(string word1); int decrypt(string word2); }; ``` ### Examples **Example 1:** ```text keys = ['a','b'], values = ["xy","xy"], dictionary = ["a","b","ab"] Calls: encrypt("ab"), decrypt("xy"), decrypt("xyxy") Results: "xyxy", 2, 1 Explanation: Both single-letter dictionary words share the first ciphertext. ``` **Example 2:** ```text keys = ['a'], values = ["zz"], dictionary = ["a","c"] Calls: encrypt("a"), decrypt("zz"), decrypt("yy") Results: "zz", 1, 0 Explanation: The unmapped dictionary word c contributes to no valid ciphertext. ``` ### Constraints - `1 <= keys.length == values.length <= 26`; keys are unique and every value has length two. - `1 <= dictionary.length <= 100`; dictionary entries are distinct and lengths lie in `[1,100]`. - `1 <= word1.length <= 2000`; word1 characters are guaranteed mapped. - word2 has even length in `[2,200]`; all strings use lowercase letters. - At most 200 total encrypt/decrypt calls. ### Clarifying Questions Different keys may share a mapped pair, so a ciphertext can have several permitted dictionary originals. Dictionary words may contain unmapped letters; only word1 is guaranteed mapped. ```hint Inspect the structure Can decryption be answered by indexing encrypted dictionary words rather than expanding every inverse mapping? ``` ### Reasoning checkpoints - Implement the specified forward mapping and count all permitted dictionary originals for a ciphertext. - Handle several keys sharing one pair without losing distinct dictionary possibilities. - Exclude words that cannot be formed from the mapping and preserve the full constructor/method protocol. ### Optional extensions How would you support adding and removing permitted dictionary words?

Overview: Count Dictionary Originals Sharing an Encryption. Implement Encrypter using distinct lowercase keys, each mapped to a two-character string; different keys may map to the same string. encrypt(word1) concatenates the mapped pairs, returning empty if any character lacks a mapping. decrypt(word2) returns the number of distinct dictionary words whose encryption equals word2. Keep the original constructor, encrypt, and integer-returning decrypt API.

Duolingo logo
Duolingo
May 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement an Encrypter object from distinct lowercase keys, their corresponding two-character values, and a dictionary of permitted original words. Different keys may share the same pair. encrypt(word1) replaces each character by its mapped pair and concatenates the pairs, returning an empty string if a character has no mapping. decrypt(word2) considers its consecutive two-character blocks: each block may be replaced by any key mapped to that pair. Return the number of distinct resulting words that occur in the dictionary. Preserve the constructor and both methods.

class Encrypter {
public:
    Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary);
    string encrypt(string word1);
    int decrypt(string word2);
};

Examples

Example 1:

keys = ['a','b'], values = ["xy","xy"], dictionary = ["a","b","ab"]
Calls: encrypt("ab"), decrypt("xy"), decrypt("xyxy")
Results: "xyxy", 2, 1
Explanation: Both single-letter dictionary words share the first ciphertext.

Example 2:

keys = ['a'], values = ["zz"], dictionary = ["a","c"]
Calls: encrypt("a"), decrypt("zz"), decrypt("yy")
Results: "zz", 1, 0
Explanation: The unmapped dictionary word c contributes to no valid ciphertext.

Constraints

  • 1 <= keys.length == values.length <= 26 ; keys are unique and every value has length two.
  • 1 <= dictionary.length <= 100 ; dictionary entries are distinct and lengths lie in [1,100] .
  • 1 <= word1.length <= 2000 ; word1 characters are guaranteed mapped.
  • word2 has even length in [2,200] ; all strings use lowercase letters.
  • At most 200 total encrypt/decrypt calls.

Clarifying Questions Guidance

Different keys may share a mapped pair, so a ciphertext can have several permitted dictionary originals. Dictionary words may contain unmapped letters; only word1 is guaranteed mapped.

Reasoning checkpoints

  • Implement the specified forward mapping and count all permitted dictionary originals for a ciphertext.
  • Handle several keys sharing one pair without losing distinct dictionary possibilities.
  • Exclude words that cannot be formed from the mapping and preserve the full constructor/method protocol.

Optional extensions

How would you support adding and removing permitted dictionary words?