当前位置:网站首页>LeetCode每日一题——890. 查找和替换模式
LeetCode每日一题——890. 查找和替换模式
2022-06-13 01:54:00 【hyk今天写算法了吗】
题目
你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配。
如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我们就得到了所需的单词,那么单词与模式是匹配的。
(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)
返回 words 中与给定模式匹配的单词列表。
你可以按任何顺序返回答案。
示例
输入:words = [“abc”,“deq”,“mee”,“aqq”,“dkd”,“ccc”], pattern = “abb”
输出:[“mee”,“aqq”]
解释: “mee” 与模式匹配,因为存在排列 {a -> m, b -> e, …}。 "ccc"与模式不匹配,因为 {a -> c, b -> c, …} 不是排列。 因为 a 和 b 映射到同一个字母。
提示:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
思路
本题考串的模式匹配问题,本题的具体要求是要从给定的字符串列表words中找出形式和给定字符串pattern格式相同的字符串集合,如pattern = ‘abc’, 从words中找到的与之相匹配的字符串也必须是三个字符各不相同的字符串如’bac’,‘xyz’等,又比如pattern = ‘aba’, 从words中找到的与之相匹配的字符串也必须是相同位置相等的字符串如’xyx’,'bab’等。因为题中所给words中字符都和pattern等长,所以具体解题可分为两种情况:
- 给定pattern长度为1,words中的字符都和pattern模式相同,直接返回words
- 给定pattern长度大于1,首先遍历pattern将其格式记录于哈希表里(从第二位开始向前面的每一位比较,生成一个布尔数组,放在哈希表里待使用),然后遍历words中的每个字符串,使用和上面同样的方法从第二位开始向前面的每一位比较,生成一个布尔数组,然后和哈希表中的数组结果相比较,如果全部相等则为答案。
题解
def findAndReplacePattern(words: List[str], pattern: str) -> List[str]:
# 情况1
if len(pattern) == 1:
return words
# 情况2
# 哈希表
temp = {
}
res = []
for i in range(1, len(pattern)):
ans = []
# 每一位朝前比较
for j in range(1, i+1):
if pattern[i] == pattern[i-j]:
ans.append(True)
else:
ans.append(False)
# 每一位朝前比较的布尔数组存于哈希表中
temp[i] = ans
for i in words:
# index记录当前比较的字符串中布尔数组和哈希表中布尔数组相同的个数
index = 0
for j in range(1, len(i)):
use = []
# 同样的方法每一位朝前比较
for k in range(1, j+1):
if i[j] == i[j-k]:
use.append(True)
else:
use.append(False)
if use == temp[j]:
index += 1
# 如果布尔数组和哈希表中的全部相等,加入答案
if index == len(i) - 1:
res.append(i)
return res
边栏推荐
- 水管工遊戲
- TensorFlow 2.x 多显卡分布式训练
- About tkinter Canvas does not display pictures
- 谷歌的智能出价有几种?
- Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)
- Introduction to common ROS commands
- Calculation of accuracy, recall rate, F1 value and accuracy rate of pytorch prediction results (simple implementation)
- rsync 传输排除目录
- Startup, connection and stop of MySQL service
- Audiences with similar interests
猜你喜欢

matplotlib画图中文乱码

(no plug-in) summary of vim basic shortcut keys

Ctrip reshapes new Ctrip

In the third quarter, the revenue and net profit increased "against the trend". What did vatti do right?

Quickly set the computer to turn off automatically

Top level configuration + cooling black technology + cool appearance, the Red Devils 6S Pro is worthy of the flagship game of the year

What is solid angle

路径字段是什么? ——竞价广告

Establishment of microservice development environment

Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
随机推荐
Répertoire d'exclusion du transport rsync
Gome's ambition of "folding up" app
Plumber game
Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
cin,cin. get(),cin. Summary of the use of getline() and getline()
Read routing table
Startup, connection and stop of MySQL service
Workspace for ROS
Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
Topic creation and running example of ROS
Torch. Distributions. Normal
The first cell of devaxpress CXGRID after inserting a row is in focus editing status
四、入库管理功能的完善
Introduction to Google unit testing tools GTEST and gmoke
Padavan mounts SMB sharing and compiles ffmpeg
MySQL - use field alias after where
What is the path field—— Competitive advertising
rsync 傳輸排除目錄
[wsl2]wsl2 migrate virtual disk file ext4 vhdx
Installing pytorch geometric