当前位置:网站首页>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
边栏推荐
- Summary of the first retrospective meeting
- How to become a white hat hacker? I suggest you start from these stages
- The Jenkins console does not output custom shell execution logs
- IP address introduction
- 20211108 A转置乘A是正定矩阵吗?A转置乘A是正定矩阵的充分必要条件是什么?
- Class loading overview
- 20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
- 20211108 det(AB)=det(A)det(B)
- Overview of common layers of image recognition neural network (under update)
- QML compilation specification
猜你喜欢

攻防世界PWN play 条件竞争漏洞的利用

线上调试工具Arthas高级

教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5

Solov2 source code analysis

20220524 how to install coppeliasim to disk D

C language: Simulated Implementation of library function strcpy

Onnx crop intermediate node

202012 CCF test questions

教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5

Mttr/mttf/mtbf diagram
随机推荐
Solov2 source code analysis
Use typescript to complete simple snake eating function
20211108 observable, controllable, stable and measurable
C language 7-13 day K candle chart (15 points)
20211108 A转置乘A是正定矩阵吗?A转置乘A是正定矩阵的充分必要条件是什么?
类的加载概述
Yolov5 model evaluation
C language: data storage in memory
MySQL startup error: innodb: operating system error number 13 in a file operation
Figure introduction to database neo4j
C language: callback function
Mttr/mttf/mtbf diagram
20211108 differential tracker
Opencv gaussianblur() explanation (Sigma value)
消息中间件
20211108 det(AB)=det(A)det(B)
202012 CCF test questions
LeetCode 583. 两个字符串的删除操作
20211005 Hermite矩阵及几个性质
Simulink的Variant Model和Variant Subsystem用法