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 subcannot be replaced more than once.
Return trueif it is possible to makesuba substring ofsby replacing zero or more characters according tomappings. Otherwise, return
false.
A substring is a contiguous non-empty sequence of characters within a string.
Input: s ="fool3e7bar", sub ="leet", mappings =[["e","3"],["t","7"],["t","8"]]Output: trueExplanation: Replace the first 'e'in sub with'3' and 't'in sub with'7'.Now sub ="l3e7"is a substring of s, so we returntrue.
Input: s ="fooleetbar", sub ="f00l", mappings =[["o","0"]]Output: falseExplanation: The string "f00l"is not a substring of s and no replacements can be made.Note that we cannot replace '0'with'o'.
Input: s ="Fool33tbaR", sub ="leetd", mappings =[["e","3"],["t","7"],["t","8"],["d","b"],["p","b"]]Output: trueExplanation: 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 returntrue.
We want to check if sub can be transformed (using the allowed mappings) into any substring of s. For each window in s of length len(sub), we try to match sub to the window, allowing each character in sub to be replaced at most once according to the mapping. We use a mapping set for fast lookup.
classSolution {
public:bool matchReplacement(string s, string sub, vector<vector<char>>& mappings) {
unordered_map<char, unordered_set<char>> mp;
for (auto& m : mappings) mp[m[0]].insert(m[1]);
int n = s.size(), m = sub.size();
for (int i =0; i + m <= n; ++i) {
bool ok = true;
for (int j =0; j < m; ++j) {
if (s[i+j] == sub[j]) continue;
if (!mp.count(sub[j]) ||!mp[sub[j]].count(s[i+j])) {
ok = false;
break;
}
}
if (ok) return true;
}
return false;
}
};
classSolution {
publicbooleanmatchReplacement(String s, String sub, char[][] mappings) {
Map<Character, Set<Character>> mp =new HashMap<>();
for (char[] m : mappings) {
mp.computeIfAbsent(m[0], k ->new HashSet<>()).add(m[1]);
}
int n = s.length(), m = sub.length();
for (int i = 0; i + m <= n; i++) {
boolean ok =true;
for (int j = 0; j < m; j++) {
if (s.charAt(i+j) == sub.charAt(j)) continue;
if (!mp.containsKey(sub.charAt(j)) ||!mp.get(sub.charAt(j)).contains(s.charAt(i+j))) {
ok =false;
break;
}
}
if (ok) returntrue;
}
returnfalse;
}
}
classSolution {
funmatchReplacement(s: String, sub: String, mappings: Array<CharArray>): Boolean {
val mp = mutableMapOf<Char, MutableSet<Char>>()
for (m in mappings) mp.getOrPut(m[0]) { mutableSetOf() }.add(m[1])
val n = s.length
val mlen = sub.length
for (i in0..n-mlen) {
var ok = truefor (j in0 until mlen) {
if (s[i+j] == sub[j]) continueif (!mp.containsKey(sub[j]) || !mp[sub[j]]!!.contains(s[i+j])) {
ok = falsebreak }
}
if (ok) returntrue }
returnfalse }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defmatchReplacement(self, s: str, sub: str, mappings: list[list[str]]) -> bool:
mp = {a: set() for a, _ in mappings}
for a, b in mappings:
mp[a].add(b)
n, m = len(s), len(sub)
for i in range(n - m +1):
ok =Truefor j in range(m):
if s[i+j] == sub[j]:
continueif sub[j] notin mp or s[i+j] notin mp[sub[j]]:
ok =Falsebreakif ok:
returnTruereturnFalse
impl Solution {
pubfnmatch_replacement(s: String, sub: String, mappings: Vec<Vec<char>>) -> bool {
use std::collections::{HashMap, HashSet};
letmut mp: HashMap<char, HashSet<char>>= HashMap::new();
for m in mappings {
mp.entry(m[0]).or_default().insert(m[1]);
}
let n = s.len();
let m = sub.len();
let s_chars: Vec<char>= s.chars().collect();
let sub_chars: Vec<char>= sub.chars().collect();
for i in0..=n-m {
letmut ok =true;
for j in0..m {
if s_chars[i+j] == sub_chars[j] {
continue;
}
if!mp.get(&sub_chars[j]).map_or(false, |set| set.contains(&s_chars[i+j])) {
ok =false;
break;
}
}
if ok {
returntrue;
}
}
false }
}