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: false
💡 Note: No position in s matches sub="f00" after applying mappings. At position 0: "foo" vs "f00" - 'f' matches 'f', but 'o' cannot be mapped to '0' (we need '0'→'o' mapping but only have 'o'→'0'). The mapping allows replacing 'o' in sub with '0', but sub already has '0' characters that need to become 'o' to match s.
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
Match Substring After Replacement INPUT s = "fool3e7bar" f o o l 3 e 7 b a r sub = "leet" l e e t mappings: "e" --> "3" "t" --> "7" "t" --> "8" Hash Map Structure: 'e': {'e', '3'} 't': {'t', '7', '8'} 'l': {'l'} ALGORITHM STEPS 1 Build Hash Map Map each char to valid replacements (Set) 2 Slide Window Try each position in s where sub could match 3 Check Each Char For sub[j], check if s[i+j] in map[sub[j]] 4 Return Result If all chars match at any position: true Matching at position 3: l 3 e 7 l=l e-->3 e=e t-->7 OK OK OK OK FINAL RESULT Match Found in s: f o o l 3 e 7 b a r Transformations Applied: l --> l (same) e --> 3 (mapped) e --> e (same) t --> 7 (mapped) Output: true Key Insight: Use a HashMap where each character in sub maps to a Set of valid replacements (including itself). This allows O(1) lookup to check if a character in s can match a character in sub. Time Complexity: O(n * m) where n = len(s), m = len(sub) TutorialsPoint - Match Substring After Replacement | Hash Map Optimization
Asked in
Facebook 15 Google 12 Amazon 8
23.4K Views
Medium Frequency
~35 min Avg. Time
892 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