Skip to content

Practice Encrypt and Decrypt Strings

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2227
LeetCode Problem

Encrypt and Decrypt Strings

Problem Statement

Encrypter(keys,values,dictionary) maps each distinct key character to a two-letter string; values may repeat. encrypt(word1) concatenates mappings, returning an empty string if a character has no mapping. decrypt(word2) counts distinct dictionary words whose encryption equals word2. It does not count all theoretically possible plaintext strings. Practice approach: Build the character map and pre-encrypt every dictionary word that can be encoded. Count identical ciphertexts in a frequency map, making decryption a lookup. This avoids exponential branching when several keys share a code. Test code collisions and dictionary entries containing unmapped characters.

Source: Encrypt and Decrypt Strings on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["Encrypter","encrypt","decrypt","decrypt"]
[[["a","b","c"],["xy","zz","xy"],["ab","cb","aa","d"]],["ab"],["xyzz"],["zzxy"]]
Output
[null,"xyzz",2,0]
Explanation

ab and cb both encrypt to xyzz. No dictionary entry encrypts to zzxy; d cannot be encoded.

Constraints

1 <= keys.length == values.length <= 26; each value has length 2.
1 <= dictionary.length <= 100; word lengths are 1 to 100.
Keys and dictionary entries are distinct; all text is lowercase.
1 <= word1.length <= 2000; generated word1 characters have mappings.
2 <= word2.length <= 200 and is even.
At most 200 total calls.

Related LeetCode Problems

Practice more problems similar to Encrypt and Decrypt Strings.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.