当前位置:网站首页>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
边栏推荐
- What is Google plus large text ads? How to use it?
- Vs how to enter chromium subprocess debugging
- 反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
- Logging system in chromium
- Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
- 移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
- Read routing table
- Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
- pringboot之restfull接口规范注解(二)
- How does Google's audience work?
猜你喜欢

指针链表的实现

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

Interruption of 51 single chip microcomputer learning notes (external interruption, timer interruption, interrupt nesting)

Stm32 mpu6050 servo pan tilt support follow

The scientific innovation board successfully held the meeting, and the IPO of Kuangshi technology ushered in the dawn

Ctrip reshapes new Ctrip
![[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register](/img/eb/9bd411be74937371de0bbf3f04267e.jpg)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register

How to solve practical problems through audience positioning?

What did Hello travel do right for 500million users in five years?

Top level configuration + cooling black technology + cool appearance, the Red Devils 6S Pro is worthy of the flagship game of the year
随机推荐
回顾ITIL各版本历程,找到企业运维发展的关键点
What is the path field—— Competitive advertising
[printf function and scanf function] (learning note 5 -- standard i/o function)
Establishment of microservice development environment
Mac下搭建MySQL环境
Introduction to ROS runtime
Use mediapipe+opencv to make a simple virtual keyboard
分享三个关于CMDB的小故事
Differences between constants and variables (detailed description) (learning note 3 -- variables and constants)
Mac使用Docker安装Oracle
CXGRID keeps the original display position after refreshing the data
Ctrip reshapes new Ctrip
Cmake has no obvious error after compilation, but prompts that pthread cannot be found
LeetCode每日一题——890. 查找和替换模式
js获取元素
Plumber game
Répertoire d'exclusion du transport rsync
STM32 timer interrupt learning notes
16 embedded C language interview questions (Classic)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register