Skip to content

Practice Find And Replace in String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#833
LeetCode Problem

Find And Replace in String

Problem Statement

Apply the replacements described by parallel arrays indices, sources and targets to the original string s. For each i, replace sources[i] with targets[i] only if sources[i] matches exactly starting at indices[i]; otherwise skip that replacement. Evaluate all matches and indices against the original s, so one replacement never shifts another. Replacements are guaranteed not to overlap. Return the resulting string.

Source: Find And Replace in String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "abcd", indices = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"]
Output
"eeebffff"

# Example 2

Input
s = "abcd", indices = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]
Output
"eeecd"

Constraints

1 <= s.length <= 1000
k == indices.length == sources.length == targets.length
1 <= k <= 100
0 <= indexes[i] < s.length
1 <= sources[i].length, targets[i].length <= 50
s consists of only lowercase English letters.
sources[i] and targets[i] consist of only lowercase English letters.

Related LeetCode Problems

Practice more problems similar to Find And Replace in String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.