当前位置:网站首页>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
边栏推荐
- Top level configuration + cooling black technology + cool appearance, the Red Devils 6S Pro is worthy of the flagship game of the year
- uniapp 预览功能
- What is solid angle
- Magics 23.0 how to activate and use the slice preview function of the view tool page
- Vivo released originos ocean, and the domestic customized system is getting better and better
- Devaxpress Chinese description --tcximagelist (enhanced image list control)
- Introduction to Google unit testing tools GTEST and gmoke
- 16 embedded C language interview questions (Classic)
- Decompression and compression of chrome resource file Pak
- (no plug-in) summary of vim basic shortcut keys
猜你喜欢

如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
![[learning notes] xr872 audio driver framework analysis](/img/1a/008a89f835dc1b350a1f1ff27bee00.jpg)
[learning notes] xr872 audio driver framework analysis

Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report

LabVIEW大型项目开发提高质量的工具

DFS and BFS to solve Treasure Island exploration

The new wild prospect of JD instant retailing from the perspective of "hour shopping"

传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)

Ten thousand words make it clear that synchronized and reentrantlock implement locks in concurrency

Devaxpress Chinese description --tdximageslider (picture rotation control)

Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)
随机推荐
Magics 23.0如何激活和使用视图工具页的切片预览功能
Matplotlib drawing Chinese garbled code
Vivo released originos ocean, and the domestic customized system is getting better and better
Combining strings and numbers using ssstream
[sequence structure, branch structure, loop structure, continue statement, break statement, return statement] (learning Note 6 -- C language process control)
[single chip microcomputer] single timer in front and back platform program framework to realize multi delay tasks
Restrict cell input type and display format in CXGRID control
Introduction to Google unit testing tools GTEST and gmoke
传感器:MQ-5燃气模块测量燃气值(底部附代码)
Why is Huawei matebook x Pro 2022 leading a "laptop" revolution
3、 Upload fabric photos to SQL server and provide name to display fabric photos
Opencv camera calibration (1): internal and external parameters, distortion coefficient calibration and 3D point to 2D image projection
华为设备配置私网IP路由FRR
Can't use typedef yet? C language typedef detailed usage summary, a solution to your confusion. (learning note 2 -- typedef setting alias)
[work notes] xr872 codec driver migration and application program example (with chip debugging method)
How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
Parameter measurement method of brushless motor
The commercial value of Kwai is being seen by more and more brands and businesses
pringboot之restfull接口规范注解(二)