Tap here to switch tabs
Problem
Submissions

Problem: Find And Replace in String

Find And Replace in String provides a string, replacement indices, source and target strings. Apply matching replacements simultaneously based on the original string and return the final string.

Statement

You are given a string s that you must perform k replacement operations on, along with three arrays of length k: indices, sources, and targets.

For each operation i:

  1. Check if the substring in s starting at index indices[i] matches sources[i].

  2. If it matches, replace that occurrence with targets[i].

  3. If it does not match, do not perform any modification for this operation.

All replacement operations occur simultaneously on the original string s, meaning that replacements do not affect the index positions or text examined by other operations.

Return the resulting string after executing all valid replacement operations.

Constraints:

  • 1≤1 \leq s.length ≤1000\leq 1000

  • k ==== indices.length ==== sources.length ==== targets.length

  • 1≤1 \leq k ≤100\leq 100

  • 0≤0 \leq indices[i] << s.length

  • 1≤1 \leq sources[i].length, targets[i].length ≤50\leq 50

  • s consists of only lowercase English letters.

  • sources[i] and targets[i] consist of only lowercase English letters.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find And Replace in String

Find And Replace in String provides a string, replacement indices, source and target strings. Apply matching replacements simultaneously based on the original string and return the final string.

Statement

You are given a string s that you must perform k replacement operations on, along with three arrays of length k: indices, sources, and targets.

For each operation i:

  1. Check if the substring in s starting at index indices[i] matches sources[i].

  2. If it matches, replace that occurrence with targets[i].

  3. If it does not match, do not perform any modification for this operation.

All replacement operations occur simultaneously on the original string s, meaning that replacements do not affect the index positions or text examined by other operations.

Return the resulting string after executing all valid replacement operations.

Constraints:

  • 1≤1 \leq s.length ≤1000\leq 1000

  • k ==== indices.length ==== sources.length ==== targets.length

  • 1≤1 \leq k ≤100\leq 100

  • 0≤0 \leq indices[i] << s.length

  • 1≤1 \leq sources[i].length, targets[i].length ≤50\leq 50

  • s consists of only lowercase English letters.

  • sources[i] and targets[i] consist of only lowercase English letters.