BackmediumStringsFlipkart

String Code Replacement Solution

Problem Statement

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.

Example 1
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'}.

Example 2
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
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

String Code Replacement — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(n)

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

Example 1

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

Example 2

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

JavaScript Solution
Time: O(n)
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;
}

Asked in Top Tech Interviews

Flipkart

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.