๐ฏ 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
subcan be replaced at most once - You can use each replacement rule multiple times
- You want to find if
subcan become a contiguous substring ofs
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
Visualization
Time & Space Complexity
For each of n starting positions in s, we check at most m characters of sub
Space for storing k mapping rules in hash map
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