当前位置:网站首页>LeetCode 6097. 替换字符后匹配(字典)
LeetCode 6097. 替换字符后匹配(字典)
2022-06-13 09:06:00 【Michael阿明】
文章目录
1. 题目
给你两个字符串 s 和 sub 。同时给你一个二维字符数组 mappings ,其中 mappings[i] = [oldi, newi] 表示你可以替换 sub 中任意数目的 oldi 字符,替换成 newi 。sub 中每个字符 不能 被替换超过一次。
如果使用 mappings 替换 0 个或者若干个字符,可以将 sub 变成 s 的一个子字符串,请你返回 true,否则返回 false 。
一个 子字符串 是字符串中连续非空的字符序列。
示例 1:
输入:s = "fool3e7bar", sub = "leet",
mappings = [["e","3"],["t","7"],["t","8"]]
输出:true
解释:将 sub 中第一个 'e' 用 '3' 替换,将 't' 用 '7' 替换。
现在 sub = "l3e7" ,它是 s 的子字符串,所以我们返回 true 。
示例 2:
输入:s = "fooleetbar", sub = "f00l",
mappings = [["o","0"]]
输出:false
解释:字符串 "f00l" 不是 s 的子串且没有可以进行的修改。
注意我们不能用 'o' 替换 '0' 。
示例 3:
输入:s = "Fool33tbaR", sub = "leetd",
mappings = [["e","3"],["t","7"],["t","8"],["d","b"],["p","b"]]
输出:true
解释:将 sub 里第一个和第二个 'e' 用 '3' 替换,
用 'b' 替换 sub 里的 'd' 。
得到 sub = "l33tb" ,它是 s 的子字符串,所以我们返回 true 。
提示:
1 <= sub.length <= s.length <= 5000
0 <= mappings.length <= 1000
mappings[i].length == 2
oldi != newi
s 和 sub 只包含大写和小写英文字母和数字。
oldi 和 newi 是大写、小写字母或者是个数字。来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/match-substring-after-replacement 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 把映射关系存入 dict,char -> set
- 滑窗遍历 s ,检查滑窗内的字符
s[i:i+len(sub)]是否都能变成 sub
from collections import defaultdict
class Solution:
def matchReplacement(self, s: str, sub: str, mappings: List[List[str]]) -> bool:
maps = defaultdict(set)
for m in mappings:
maps[m[0]].add(m[1])
for i in range(0, len(s)-len(sub)+1):
flag = True
for j in range(len(sub)):
if s[i+j] == sub[j]: # 一样不需要操作
continue
if s[i+j] not in maps[sub[j]]: # 不能变成 sub
flag = False
break
if flag:
return True
return False2856 ms 15.7 MB Python3
边栏推荐
- Talking about acid of database
- JUC atomic reference and ABA problem
- Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
- How to become a white hat hacker? I suggest you start from these stages
- LeetCode 201. 数字范围按位与
- C language time difference calculation
- A very detailed blog about the implementation of bilinear interpolation by opencv
- JUC原子整数
- Neo4j environment construction
- 20220524 how to install coppeliasim to disk D
猜你喜欢

C language: deep understanding of pointers and arrays

turtle库显示系统时间

CAS NO lock

线上调试工具Arthas基础

Online debugging tool Arthas advanced

Redirect vulnerability analysis of network security vulnerability analysis

Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5

Completely uninstall PostgreSQL under Linux

Final principle

BGP 联邦+Community
随机推荐
Batch read all voice files under the folder
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
Spectre record
Two good kids
Immutability of shared model
JUC atomic accumulator
Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
类的加载概述
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
20211020 academician all drive system
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
简单实现数据库链接池
C language: callback function
C/s model and P2P model
Opencv gaussianblur() explanation (Sigma value)
JUC 原子累加器
turtle库显示系统时间
攻防世界-PWN-shell
20211108 det(AB)=det(A)det(B)
Jfinal and swagger integration