Match Substring After Replacement LeetCode solution in Python

1
from collections import defaultdict
2
import re
3

4

5
class Solution:
6
def matchReplacement(self, s: str, sub: str, mappings: List[List[str]]) -> bool:
7
reachable = defaultdict(set)
8
for a, b in mappings:
9
reachable[a].add(b)
10
for c in sub:
11
reachable[c].add(c)
12
regex = ""
13
for c in sub:
14
if len(reachable[c]) > 1:
15
regex += "("
16
regex += "|".join(reachable[c])
17
regex += ")"
18
else:
19
regex += c
20
return re.compile(regex).search(s)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0