Medium · Strings

Find and Replace in String

Given a string s and k operations (indices[i], sources[i], targets[i]), replace sources[i] with targets[i] wherever sources[i] occurs in the original s starting at indices[i], applying all such replacements simultaneously, and return the resulting string; indices may be unsorted or repeated, and the replacements that apply never overlap.

Examples

Example 1

{
  "s": "abcd",
  "indices": [0, 2],
  "sources": ["a", "cd"],
  "targets": ["eee", "ffff"]
}

Output: "eeebffff"

Example 2

{
  "s": "abcd",
  "indices": [0, 2],
  "sources": ["ab", "ec"],
  "targets": ["eee", "ffff"]
}

Output: "eeecd"

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Strings problems