当前位置:网站首页>Leetcode daily question - 890 Find and replace mode
Leetcode daily question - 890 Find and replace mode
2022-06-13 02:02:00 【Did HYK write the algorithm today】
List of articles
subject
You have a list of words words And a pattern pattern, Do you want to know words Which words in match the pattern .
If there is an arrangement of letters p , Make every letter in the pattern x Replace with p(x) after , We get the words we need , So the words match the patterns .
( Think about it , The arrangement of letters is from letter to letter : Each letter maps to another letter , No two letters map to the same letter .)
return words List of words matching the given pattern in .
You can return the answers in any order .
Example
Input :words = [“abc”,“deq”,“mee”,“aqq”,“dkd”,“ccc”], pattern = “abb”
Output :[“mee”,“aqq”]
explain : “mee” Match pattern , Because there are permutations {a -> m, b -> e, …}. "ccc" Does not match pattern , because {a -> c, b -> c, …} It's not a permutation . because a and b Map to the same letter .
Tips :
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
Ideas
The pattern matching problem of the test string of this question , The specific requirement of this question is to list the given strings words Find the form and given string in pattern A collection of strings in the same format , Such as pattern = ‘abc’, from words The matching string found in must also be a string with three different characters, such as ’bac’,‘xyz’ etc. , And such as pattern = ‘aba’, from words The matching string found in must also be a string with the same position, such as ’xyx’,'bab’ etc. . Because the question gives words Both Chinese characters and pattern Equal length , So the specific problem-solving can be divided into two cases :
- Given pattern The length is 1,words The characters in are all the same as pattern Same pattern , Go straight back to words
- Given pattern Longer than 1, First traverse pattern Record the format in the hash table ( Start from the second place and compare with each one in front , Generate a Boolean array , Put it in the hash table for use ), Then traverse words Each string in , Use the same method as above to compare from the second bit to each of the previous bits , Generate a Boolean array , Then compare with the array result in the hash table , If all are equal, it is the answer .
Answer key
def findAndReplacePattern(words: List[str], pattern: str) -> List[str]:
# situation 1
if len(pattern) == 1:
return words
# situation 2
# Hashtable
temp = {
}
res = []
for i in range(1, len(pattern)):
ans = []
# Each one of them looks forward and compares
for j in range(1, i+1):
if pattern[i] == pattern[i-j]:
ans.append(True)
else:
ans.append(False)
# The Boolean array for each bit forward comparison is stored in the hash table
temp[i] = ans
for i in words:
# index Record the same number of Boolean arrays in the currently compared string and the hash table
index = 0
for j in range(1, len(i)):
use = []
# In the same way, each one of them looks forward
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 the Boolean array and the hash table are all equal , Add answers
if index == len(i) - 1:
res.append(i)
return res
边栏推荐
- swiper 横向轮播 grid
- TensorFlow 2. X multi graphics card distributed training
- Numpy multidimensional array transpose transpose
- C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。
- Rsync transport exclusion directory
- Using atexit to realize automatic destruct of singleton mode
- Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
- [work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip
- STM32 steering gear controller
- [the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer
猜你喜欢
指针链表的实现
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
(no plug-in) summary of vim basic shortcut keys
如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
Implementation of pointer linked list
General IP address, account and password of mobile IPv6 optical cat login, and mobile optical cat is in bridging mode
Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
Introduction to ROS runtime
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
随机推荐
Why is Huawei matebook x Pro 2022 leading a "laptop" revolution
Use mediapipe+opencv to make a simple virtual keyboard
【Unity】打包WebGL项目遇到的问题及解决记录
C language volatile learning
Pyflink implements custom sourcefunction
华为设备配置虚拟专用网FRR
Rsync transport exclusion directory
Application and examples of C language structure
Detailed explanation of C language conditional compilation
Establishment of microservice development environment
Decompression and compression of chrome resource file Pak
Stm32+ze-08 formaldehyde sensor tutorial
分享三个关于CMDB的小故事
传感器:MQ-5燃气模块测量燃气值(底部附代码)
Répertoire d'exclusion du transport rsync
Shell command notes
移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
[programming idea] communication interface of data transmission and decoupling design of communication protocol
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()