#2301
Match Substring After Replacement
master · 1630 · lc hard +32 · premium · verified · 43.2% accepted · 394 likes · top 25%
Description
You are given two strings s and sub, and a 2D character array mappings where mappings[i] = [oldi, newi] means you may replace any occurrence of character oldi in sub with newi. Each character position in sub can be substituted at most once.
Return true if some sequence of allowed substitutions can turn sub into a substring of s, otherwise return false.
Example 1:
Input: s = "fool3e7bar", sub = "leet", mappings = [["e","3"],["t","7"],["t","8"]]
Output: true
Explanation: Replace the first 'e' in sub with '3' and 't' in sub with '7'.
Now sub = "l3e7" is a substring of s, so we return true.
Example 2:
Input: s = "fooleetbar", sub = "f00l", mappings = [["o","0"]]
Output: false
Explanation: The string "f00l" is not a substring of s and no replacements can be made.
Note that we cannot replace '0' with 'o'.
Example 3:
Input: s = "Fool33tbaR", sub = "leetd", mappings = [["e","3"],["t","7"],["t","8"],["d","b"],["p","b"]]
Output: true
Explanation: Replace the first and second 'e' in sub with '3' and 'd' in sub with 'b'.
Now sub = "l33tb" is a substring of s, so we return true.
Code
1
2
3