Match Substring After Replacement LeetCode solution in Python
1from collections import defaultdict2import re345class Solution:6def matchReplacement(self, s: str, sub: str, mappings: List[List[str]]) -> bool:7reachable = defaultdict(set)8for a, b in mappings:9reachable[a].add(b)10for c in sub:11reachable[c].add(c)12regex = ""13for c in sub:14if len(reachable[c]) > 1:15regex += "("16regex += "|".join(reachable[c])17regex += ")"18else:19regex += c20return re.compile(regex).search(s)