当前位置:网站首页>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 False
2856 ms 15.7 MB Python3
边栏推荐
- 20220606 Young's inequality for Matrices
- Download address of QT source code of each version
- 批量读取文件夹下的全部语音文件
- 20211005 Hermite matrix and some properties
- Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
- JUC atomic integer
- 类的加载概述
- Z字形变换
- LeetCode 201. 数字范围按位与
- LeetCode 202. 快乐数
猜你喜欢
Yolov5 face learning notes
Final principle
Neo4j - CQL使用
Some websites of QT (software download, help documents, etc.)
QT multithreaded TCP server
[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation
JUC 原子累加器 源码之 LongAdder
20220524 如何把CoppeliaSim安装到D盘
an error occurred while trying to rename a file in the destination directory code 5
The Jenkins console does not output custom shell execution logs
随机推荐
Download address of QT source code of each version
Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5
批量读取文件夹下的全部语音文件
CAS NO lock
20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
The Jenkins console does not output custom shell execution logs
Summary of the first retrospective meeting
20211104 为什么相似矩阵的迹相同
JUC atomic reference and ABA problem
教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5
C language: shortcut keys commonly used in VS
消息中间件
Neo4j環境搭建
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
Yolov5 face learning notes
Opencv gaussianblur() explanation (Sigma value)
JUC原子整数
20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
[network security penetration] if you don't understand CSRF? This article gives you a thorough grasp
How excel adds hyperlinks to some text in a cell