Match Substring After Replacement - Problem
You are given two strings s and sub. You are also given a 2D character array mappings where mappings[i] = [oldi, newi] indicates that you may perform the following operation any number of times:
Replace a character oldi of sub with newi.
Each character in sub cannot be replaced more than once.
Return true if it is possible to make sub a substring of s by replacing zero or more characters according to mappings. Otherwise, return false.
A substring is a contiguous non-empty sequence of characters within a string.
Input & Output
Example 1 — Basic Match with Replacement
$
Input:
s = "fool3e7bar", sub = "leet", mappings = [["e","3"],["t","7"],["t","8"]]
›
Output:
true
💡 Note:
At position 2: "ol3e" matches "leet" by replacing e→3 in sub, giving "le37" which doesn't work. At position 2: we need to match s[2:6]="ol3e" with sub="leet". We can replace 'e'→'3' and 't'→'7' to get "le37", but this doesn't match "ol3e". Actually, at position 2: s="ool3" vs sub="leet" - this doesn't work. Let me recalculate: we need s[i:i+4] to match sub after replacements.
Example 2 — No Valid Match
$
Input:
s = "fooleetbar", sub = "f00", mappings = [["o","0"]]
›
Output:
true
💡 Note:
At position 0: "foo" matches "f00" by replacing both o→0 in sub, giving "f00" which matches s[0:3]="foo" when we replace o with 0 in sub.
Example 3 — Exact Match
$
Input:
s = "abcd", sub = "bc", mappings = []
›
Output:
true
💡 Note:
At position 1: "bc" in s exactly matches sub "bc" without any replacements needed.
Constraints
- 1 ≤ sub.length ≤ s.length ≤ 5000
- 0 ≤ mappings.length ≤ 1000
- mappings[i].length == 2
- s and sub consist of lowercase English letters and digits
- mappings[i] consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
String s, substring sub, and character mappings
2
Process
Try each position in s, use mappings to transform sub characters
3
Output
Return true if any position allows sub to match as substring
Key Takeaway
🎯 Key Insight: Use character mappings to transform sub and check if it matches any substring of s at each position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code