Match Substring After Replacement - Problem

๐ŸŽฏ Match Substring After Replacement

You're given a string s and a substring pattern sub that you want to find within s. However, there's a twist! You have a set of character replacement rules in the form of a 2D array mappings, where each mappings[i] = [old_char, new_char] allows you to replace any occurrence of old_char in sub with new_char.

The Challenge: Can you transform sub using these replacement rules to make it appear as a contiguous substring somewhere in s?

Important Rules:

  • Each character in sub can be replaced at most once
  • You can use each replacement rule multiple times
  • You want to find if sub can become a contiguous substring of s

Example: If s = "fool3e7bar", sub = "leet", and mappings = [['e','3'], ['t','7'], ['t','8']], you can transform "leet" โ†’ "le37" by replacing the first 'e' with '3' and 't' with '7', making it match the substring "le37" in s.

Input & Output

example_1.py โ€” Basic Transformation
$ Input: s = "fool3e7bar", sub = "leet", mappings = [['e','3'], ['t','7'], ['t','8']]
โ€บ Output: true
๐Ÿ’ก Note: We can transform 'leet' to 'le37' by replacing the first 'e' with '3' and 't' with '7'. The string 'le37' appears as a substring in 'fool3e7bar' at position 3.
example_2.py โ€” No Valid Transformation
$ Input: s = "fooleetbar", sub = "f00", mappings = [['o','0']]
โ€บ Output: false
๐Ÿ’ก Note: We can transform 'f00' to 'f0o' by replacing one 'o' with '0', but we cannot replace both 'o's since each character can only be replaced once. No transformation of 'f00' appears in 'fooleetbar'.
example_3.py โ€” Direct Match
$ Input: s = "Fool33tboR", sub = "leetb", mappings = [['e','3'], ['t','R'], ['O','b']]
โ€บ Output: false
๐Ÿ’ก Note: Even with the available mappings, no transformation of 'leetb' can match any substring in 'Fool33tboR'. The closest would be transforming to 'l33tR' but this doesn't appear as a contiguous substring.

Visualization

Tap to expand
Sliding Window Pattern MatchingDocument (string s):fool3e7barMatch Found!Template (sub pattern):leetTransformation Rules:e โ†’ 3t โ†’ 7t โ†’ 8โœ“ Each character in template can be transformed at most onceMatching Process:1. l โ†” l (direct match) โœ“2. e โ†” 3 (transform) โœ“3. e โ†” e (direct match) โœ“4. t โ†” 7 (transform) โœ“Result: Pattern matches at position 3!
Understanding the Visualization
1
Prepare Tools
Build a lookup table of all available character transformations from the mappings
2
Slide Template
Position the template at each possible location in the document
3
Character Match
For each template position, compare characters one by one
4
Apply Rules
If characters don't match directly, check if transformation rules allow the match
5
Success Check
If all characters in template can be matched, return success
Key Takeaway
๐ŸŽฏ Key Insight: Instead of generating all possible transformations (exponential), we check each position in the target string and see if we can match the pattern using available transformations on-the-fly (linear).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m)

For each of n starting positions in s, we check at most m characters of sub

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for storing k mapping rules in hash map

n
2n
โœ“ Linear Space

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 uppercase and lowercase English letters and digits
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 18
24.5K Views
Medium Frequency
~25 min Avg. Time
943 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen