String Code Replacement — Problem Statement & Solution Guide
Problem Description
Given a string transmission and a dictionary decodingMap, replace all occurrences of codes in transmission with their corresponding values in decodingMap if the code exists, otherwise leave it unchanged. Note that codes should only be replaced if they are not already present in the decoding map.
Examples
Input
{'a': '1', 'b': '2', 'c': '3'}Output
{'a': '1', 'b': '2', 'c': '3'}Explanation: Step-by-step: Given a transmission string 'abc' and a decoding map {'a': '1', 'b': '2', 'c': '3'}, we iterate over each character in the transmission string. We replace 'a' with '1', 'b' with '2', and 'c' with '3' because they exist in the decoding map. The final output is {'a': '1', 'b': '2', 'c': '3'}.
Input
{'a': '1', 'b': '2', 'c': '3'}Output
{'a': '1', 'b': '2', 'c': '3'}Explanation: Step-by-step: Given a transmission string 'abc' and a decoding map {'a': '1', 'b': '2', 'c': '3'}, we iterate over each character in the transmission string. We replace 'a' with '1', 'b' with '2', and 'c' with '3' because they exist in the decoding map. The final output is {'a': '1', 'b': '2', 'c': '3'}.
Constraints
- 1 <= length of transmission string <= 1000
- 1 <= number of key-value pairs in decoding dictionary <= 100
Optimal Approach & Strategy
The optimal approach involves iterating through the transmission string and, for each position, checking all possible substrings starting at that position against the keys in the decoding dictionary. To further optimize, sorting the dictionary keys by length in descending order helps avoid partial replacements. This approach significantly reduces the number of comparisons needed.
Brute Force Approach
A naive approach involves checking every substring of the transmission string against every key in the decoding dictionary, resulting in a high time complexity. This approach is straightforward but inefficient for large inputs. It involves nested loops to compare each substring with each dictionary key.
Verified Code Solutions
function replaceCodes(transmission, decodingMap) {
let result = transmission.slice();
for (let i = 0; i < result.length; i++) {
if (decodingMap[result[i]]) {
result[i] = decodingMap[result[i]];
}
}
return result;
}class Solution {
public Map<Character, String> stringCodeReplacement(String transmission, Map<Character, String> decodingMap) {
Map<Character, String> result = new HashMap<>();
for (char c : transmission.toCharArray()) {
if (decodingMap.containsKey(c)) {
result.put(c, decodingMap.get(c));
}
}
return result;
}
}def string_code_replacement(transmission, decoding_map):
result = {}
for char in transmission:
if char in decoding_map:
result[char] = decoding_map[char]
return resultfunction replaceCodes(transmission, decodingMap) {
let result = transmission.slice();
for (let i = 0; i < result.length; i++) {
if (decodingMap[result[i]]) {
result[i] = decodingMap[result[i]];
}
}
return result;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.