The Encrypt and Decrypt Strings coding problem involves designing a system that can transform strings based on a provided mapping.
keys and values array.dictionary would result in that exact encrypted string when encrypted.
You need to implement the encrypt and decrypt functions efficiently.Companies like Amazon and Google use this problem to test a candidate's ability to optimize lookup operations. While encryption is a direct mapping (O(N)), decryption is more complex because many different strings might encrypt to the same result. The Hash Table interview pattern or a Trie interview pattern is usually needed to precompute dictionary encryptions and answer decryption queries in constant time.
This is a Design and Precomputation problem.
char -> 2-char string. This makes encrypt O(Length of string).dictionary during initialization.encrypted_string -> count).Keys: ['a', 'b'], Values: ["11", "22"]. Dictionary: ["aa", "ab"].
{"1111": 1, "1122": 1}.decrypt("1111") returns 1.decrypt("1122") returns 1.decrypt("3333") returns 0.decrypt function fast enough for multiple calls.In "Design" problems, if one function (decrypt) seems very hard or slow, look for ways to move the work into the constructor or the other function (encrypt). This "heavy initialization, light query" trade-off is a common design pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Prefix and Suffix Search | Hard | Solve | |
| Palindrome Pairs | Hard | Solve | |
| Replace Words | Medium | Solve | |
| Design SQL | Medium | Solve | |
| Unique Word Abbreviation | Medium | Solve |