当前位置:网站首页>[890. find and replace mode]
[890. find and replace mode]
2022-06-12 22:20:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
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 <= 501 <= pattern.length = words[i].length <= 20
Method : Constructive bijection
We can judge one by one words Every word in word Whether or not pattern matching .
According to the meaning , We need to construct a bijection from letter to letter , namely word Each letter of needs to be mapped to pattern Corresponding letter of , also pattern Each letter of also needs to be mapped to word Corresponding letter of .
We can write a function match(word,pattern), Only when the word The same letters in the map to pattern Returns... When the same letter in true. We can traverse these two strings while , Use a hash table to record word Every letter of x Need to map to pattern Which letter of , If x Already mapped , You need to check whether the corresponding letters are the same .
If match(word,pattern) and match(pattern,word) Are all true, said word And pattern matching .
Code :
class Solution {
bool match(string &word, string &pattern) {
unordered_map<char, char> mp;
for (int i = 0; i < word.length(); ++i) {
char x = word[i], y = pattern[i];
if (!mp.count(x)) {
mp[x] = y;
} else if (mp[x] != y) {
// word The same letter in must be mapped to pattern On the same letter in
return false;
}
}
return true;
}
public:
vector<string> findAndReplacePattern(vector<string> &words, string &pattern) {
vector<string> ans;
for (auto &word: words) {
if (match(word, pattern) && match(pattern, word)) {
ans.emplace_back(word);
}
}
return ans;
}
};
Execution time :4 ms, In all C++ Defeated in submission 66.05% Users of
Memory consumption :8.2 MB, In all C++ Defeated in submission 59.88% Users of
Complexity analysis
Time complexity : O(nm), among n It's an array words The length of ,m yes pattern The length of . For each word need O(m) Time to check if it is consistent with pattern matching .
Spatial complexity : O(m). Hash table needs O(m) Space .
author:LeetCode-Solution
边栏推荐
- C语言:如何给全局变量起一个别名?
- [machine learning] learning notes 01- introduction
- China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
- [proteus simulation] simple digital tube timer clock
- Leetcode Yanghui triangle
- 【LeetCode】53.最大子数组和
- Research Report on truffle fungus industry - market status analysis and development prospect forecast
- 在同花顺开户安全么 ,证券开户怎么开户流程
- You can move forward or backward. This function in idea is amazing!
- Logstash timestamp converted to UNIX nanosecond nano second time
猜你喜欢

How to prevent phishing emails? S/mime certificate to help!

Redis optimization
![[C language] data type occupation](/img/12/e0f9679076d89fb5bd993ee3c345bf.jpg)
[C language] data type occupation

The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables

SQL query list all views in SQL Server 2005 database - SQL query to list all views in an SQL Server 2005 database
![[probability theory and mathematical statistics] final review: formula summary and simple examples (end)](/img/f5/1c8392aaf87ea323524e94e3f213ed.png)
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)

Use group_ Dplyr issues when using group_ by(multiple variables)

JVM Basics - > how GC determines that an object can be recycled

Dolphin-2.0.3 cluster deployment document

Ansible playbook和Ansible Roles(三)
随机推荐
Thread safe level
Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
Is it safe to open an account in tonghuashun? How to open an account for securities
Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
动态规划之如何将问题抽象转化为0-1背包问题(详解利用动态规划求方案数)
【LeetCode】剑指 Offer II 020. 回文子字符串的个数
【Proteus仿真】简易数码管定时器时钟
Configuring Dingding notification of SQL audit platform archery
China embolic coil market trend report, technical innovation and market forecast
PE installation win10 system
Leetcode Yanghui triangle
设计消息队列存储消息数据的 MySQL 表格
Ansible playbook and variable (II)
[sword finger offer simple] sword finger offer 24 Reverse linked list
What is the difference between a user thread and a daemon thread?
2021 rust survey results released: 9354 questionnaires collected
认识的几位清华同学都离职了……
JVM Basics - > how to troubleshoot JVM problems in your project
NoSQL - redis configuration and optimization (II) high availability, persistence and performance management
The kotlin coroutine -- coroutine context and exception propagation