Encrypt and Decrypt Strings - Problem

πŸ” Encrypt and Decrypt Strings

You're building a custom encryption system that can both encrypt and decrypt strings using a special mapping scheme. This system is unique because decryption might have multiple valid interpretations!

Given:

  • A keys array of unique characters (the alphabet of your cipher)
  • A values array of 2-character strings (the encrypted representations)
  • A dictionary array of valid words that can result from decryption

Encryption Process:

For each character c in the input string:

  1. Find the index i where keys[i] == c
  2. Replace c with values[i]
  3. If any character isn't found in keys, return ""

Decryption Process:

For each 2-character substring at even positions:

  1. Find which values[i] matches this substring
  2. Replace it with keys[i]
  3. Count how many possible decryptions appear in the dictionary

Example: If keys = ['a', 'b'] and values = ["01", "10"], then "ab" encrypts to "0110" and "0110" could decrypt back to "ab" (if "ab" is in the dictionary).

Input & Output

example_1.py β€” Basic Encryption and Decryption
$ Input: keys = ['a', 'b', 'c'], values = ["ei", "zf", "ei"], dictionary = ["abcdefghijklmnop"] Operations: - encrypt("abcdefghijklmnop") - decrypt("eizfeiei")
β€Ί Output: encrypt: "eizfeiei" decrypt: 1
πŸ’‘ Note: Encryption maps each character using the keys array: 'a'β†’'ei', 'b'β†’'zf', 'c'β†’'ei', etc. The string "abcdefghijklmnop" becomes "eizfeiei...". For decryption, "eizfeiei" can decode back to words that appear in the dictionary, and there's 1 such word.
example_2.py β€” Multiple Possible Decryptions
$ Input: keys = ['a', 'b'], values = ["01", "10"], dictionary = ["ab", "ba"] Operations: - encrypt("ab") - decrypt("0110")
β€Ί Output: encrypt: "0110" decrypt: 2
πŸ’‘ Note: Both "ab" and "ba" encrypt to "0110" since 'a'β†’'01' and 'b'β†’'10'. When decrypting "0110", it can be split as "01"+"10" (giving "ab") and both words are in the dictionary, so the count is 2.
example_3.py β€” Character Not in Keys
$ Input: keys = ['a', 'b'], values = ["01", "10"], dictionary = ["abc"] Operations: - encrypt("abc") - decrypt("01")
β€Ί Output: encrypt: "" decrypt: 0
πŸ’‘ Note: The character 'c' is not in the keys array, so encryption of "abc" fails and returns empty string. "01" has odd length so cannot be properly decrypted (each encrypted char is 2 digits), resulting in 0 possible decryptions.

Visualization

Tap to expand
πŸ•΅οΈ Secret Agent Cipher SystemπŸ“– Code BookEncrypt Map:'a' β†’ '01''b' β†’ '10''c' β†’ '11'O(1) lookupπŸ“‹ Approved WordsPreprocessed:'0110' β†’ 2 words'0111' β†’ 1 word'1011' β†’ 1 wordPre-encrypted⚑ OperationsEncrypt: O(n)Decrypt: O(1)where n = word lengthLightning fast!Message: "ab"EncryptCode: "0110"DecryptCount: 2🎯 Key Insight:Instead of trying all possible decryptions (exponential time),we preprocess the dictionary by encrypting each word once.This turns decrypt from O(exponential) to O(1)!πŸ’‘Preprocessing is the key to efficiency!
Understanding the Visualization
1
Build Code Book
Create hash maps: char→code and code→[possible chars]
2
Preprocess Dictionary
Encrypt all approved words and store their counts
3
Encrypt Message
For each character, lookup its code in O(1) time
4
Decrypt Message
Lookup encrypted string in preprocessed dictionary counts
Key Takeaway
🎯 Key Insight: By preprocessing the dictionary (encrypting all words during initialization), we transform the decrypt operation from exponential backtracking to a simple O(1) hash table lookup, making the system blazingly fast for real-time encryption and decryption operations.

Time & Space Complexity

Time Complexity
⏱️
O(1) encrypt, O(1) decrypt after O(D*L) preprocessing

D is dictionary size, L is average word length. Each operation is O(1) after preprocessing

n
2n
βœ“ Linear Growth
Space Complexity
O(K + D*L)

K for key-value mappings, D*L for storing encrypted dictionary words

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ keys.length == values.length ≀ 26
  • values[i].length == 2
  • 1 ≀ dictionary.length ≀ 100
  • 1 ≀ dictionary[i].length ≀ 100
  • At most 200 calls will be made to encrypt and decrypt
  • keys contains unique characters
  • All characters in dictionary[i] are in keys
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.9K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen