To solve this problem, we need to encrypt a given string based on a specific mapping derived from examples provided. The encryption involves replacing certain lowercase letters with predefined characters while leaving all other characters (including numbers, special symbols, and unmapped letters) unchanged.
Approach
Problem Analysis: The problem requires encrypting strings where specific lowercase letters are mapped to other characters (either letters or digits) as per given examples. For instance, ‘a’ maps to ‘s’, ‘b’ maps to ‘6’, ‘d’ maps to ‘v’, etc. All other characters remain unchanged.
Mapping Dictionary: We create a dictionary that contains the predefined mappings for the letters observed in the examples. This dictionary includes:
— ‘a’ → ‘s’
— ‘b’ → ‘6’
— ‘d’ → ‘v’
─ ‘e’ → ‘w’
— ‘h’ → ‘3’
─ ‘i’ → ‘0’
─ ‘m’ → ‘4’
─ ‘n’ → ‘5’
─ ‘o’ → ‘6’
, ‘r’ → ‘9’
─ ‘t’ → ‘z’
─ ‘y’ → ‘g’
Processing Input: For each test case, we read the input string, process each character by replacing it with its mapped value if it exists in the dictionary; otherwise, we leave the character unchanged.
Output: The encrypted string is printed for each test case.
Solution Code
t = int(input.strip)
mapping = {
‘a’: ‘s’,
‘b’: ‘6’,
‘d’: ‘v’,
‘e’: ‘w’,
‘h’: ‘3’,
‘i’: ‘0’,
‘m’: ‘4’,
‘n’: ‘5’,
‘o’: ‘6’,
‘r’: ‘9’,
‘t’: ‘z’,
‘y’: ‘g’
}
for _ in range(t):
s = input
res = []
for char in s:
if char in mapping:
res.append(mapping[char])
else:
res.append(char)
print(».join(res))
Explanation
Reading Input: The first line of input is the number of test cases, `t`.
Mapping Setup: A dictionary `mapping` is defined to store the predefined character mappings derived from the problem examples.
Processing Each String: For each test case:
─ The input string is read.
— Each character in the string is checked against the `mapping` dictionary. If the character is found in the dictionary, it is replaced by its corresponding value; otherwise, it remains unchanged.
─ The processed characters are joined into a string and printed as the encrypted result.
This approach efficiently handles the encryption by leveraging a predefined mapping for specific characters while ensuring all other characters are left unaltered, thus meeting the problem requirements. The solution is optimal with a time complexity of O(n) per test case, where n is the length of the input string.