Apply Matching String Replacements Simultaneously

Quick Overview

Apply Matching String Replacements Simultaneously. Parallel arrays indices,sources,targets describe replacements in the original string s.

Apply Matching String Replacements Simultaneously

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Parallel arrays indices,sources,targets describe replacements in the original string s. At each indices[i], replace sources[i] with targets[i] only if the source exactly matches there. Otherwise do nothing. Successful replacement spans do not overlap. All matching and indexing refer to the original string; return the combined result. ```cpp class Solution { public: string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets); }; ``` ### Examples **Example 1:** ```text s = "planet", indices = [0,4], sources = ["pl","et"], targets = ["b","ing"] Output: "baning" Explanation: Original pl becomes b and original et becomes ing, leaving an in the middle. ``` **Example 2:** ```text s = "house", indices = [1], sources = ["aa"], targets = ["x"] Output: "house" Explanation: The original substring at1 is ou, so no replacement occurs. ``` ### Constraints - Original string length is 1 through 1000. - All three operation arrays have the same length k in [1,100]. - Every index is in [0,s.length-1]. - Each source and target has 1 through 50 characters. - All strings use lowercase English letters; successful replacement spans do not overlap. ### Contract considerations Preserve original indices and simultaneous nonoverlapping semantics. ```hint Read the boundary condition Can successful replacements be marked before constructing any output? ``` ### Boundary reminders Preserve original indices and simultaneous nonoverlapping semantics.

Overview: Apply Matching String Replacements Simultaneously. Parallel arrays indices,sources,targets describe replacements in the original string s.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Parallel arrays indices,sources,targets describe replacements in the original string s. At each indices[i], replace sources[i] with targets[i] only if the source exactly matches there. Otherwise do nothing. Successful replacement spans do not overlap. All matching and indexing refer to the original string; return the combined result.

class Solution {
public:
    string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets);
};

Examples

Example 1:

s = "planet", indices = [0,4], sources = ["pl","et"], targets = ["b","ing"]
Output: "baning"
Explanation: Original pl becomes b and original et becomes ing, leaving an in the middle.

Example 2:

s = "house", indices = [1], sources = ["aa"], targets = ["x"]
Output: "house"
Explanation: The original substring at1 is ou, so no replacement occurs.

Constraints

  • Original string length is 1 through 1000.
  • All three operation arrays have the same length k in [1,100].
  • Every index is in [0,s.length-1].
  • Each source and target has 1 through 50 characters.
  • All strings use lowercase English letters; successful replacement spans do not overlap.

Contract considerations

Preserve original indices and simultaneous nonoverlapping semantics.

Boundary reminders

Preserve original indices and simultaneous nonoverlapping semantics.