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.

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

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.
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