Skip to content

Practice Match Substring After Replacement

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

Hard#2301
LeetCode Problem

Match Substring After Replacement

Problem Statement

Given s, sub, and directed character mappings [old, new], you may replace any occurrence of old in sub with new. Each character position may be replaced at most once, so mappings cannot be chained at the same position or reversed unless separately supplied. Return whether sub can become a contiguous substring of s after zero or more replacements.

Source: Match Substring After Replacement on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "fool3e7bar", sub = "leet", mappings = [["e","3"],["t","7"],["t","8"]]
Output
true

# Example 2

Input
s = "fooleetbar", sub = "f00l", mappings = [["o","0"]]
Output
false

# Example 3

Input
s = "Fool33tbaR", sub = "leetd", mappings = [["e","3"],["t","7"],["t","8"],["d","b"],["p","b"]]
Output
true

Constraints

1 <= sub.length <= s.length <= 5000
0 <= mappings.length <= 1000
mappings[i].length == 2
oldi != newi
s and sub consist of uppercase and lowercase English letters and digits.
oldi and newi are either uppercase or lowercase English letters or digits.

Related LeetCode Problems

Practice more problems similar to Match Substring After Replacement.

Review your practice with PhantomCodeAI

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