当前位置:网站首页>890. Find and Replace Pattern
890. Find and Replace Pattern
2022-06-13 05:17:00 【SUNNY_CHANGQI】
The description of the problem
Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-and-replace-pattern
an example
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {
a -> m, b -> e, ...}.
"ccc" does not match the pattern because {
a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-and-replace-pattern
The codes for above problem
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for (auto &word : words) {
if (isMatch(word, pattern)) {
res.push_back(word);
}
}
return res;
}
bool isMatch(string& word, string& pattern){
if (word.size() != pattern.size()) return false;
vector<int> map(26, -1);
for (int i = 0; i < word.size(); i++) {
if (map[word[i] - 'a'] == -1) {
// check the pattern[i] - 'a' is in the map or not
if (find(map.begin(), map.end(), pattern[i] - 'a') != map.end()) {
return false;
}
map[word[i] - 'a'] = pattern[i] - 'a';
} else if (map[word[i] - 'a'] != pattern[i] - 'a') {
return false;
}
}
return true;
}
};
int main()
{
Solution s;
vector<string> words = {
"abc","deq","mee","aqq","dkd","ccc"};
string pattern = "abb";
vector<string> res = s.findAndReplacePattern(words, pattern);
std::cout << "res: ";
for (auto &word : res) {
std::cout << word << " ";
}
std::cout << std::endl;
return 0;
}
The corresponding results
$ ./test
res: mee aqq
边栏推荐
- Float type value range
- Chapter 15 mechanism: Address Translation
- std::condition_ variable::wait_ for
- What is the saturate operation in opencv
- Mysql database backup and restore:
- 通过命令行创建harbor镜像库
- Small project - household income and expenditure software (1)
- [multi thread programming] the future interface obtains thread execution result data
- Jeffery0207 blog navigation
- Metaltc4.0 stable release
猜你喜欢
Dup2 use
Binary search and binary answer
Mysql database crud operation
metaRTC4.0稳定版发布
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
Listiterator list iterator
Bomb disposal cat
Sort (internal sort) + external sort
Dynamic and static libraries
SQL table columns and statements of database
随机推荐
QT direction key to move focus
COAP protocol libcoap API
Case - the ArrayList collection stores student objects and traverses them in three ways
RT thread console device initialization
Time display of the 12th Blue Bridge Cup
C language learning log 10.5
C language learning log 1.17
Building Nacos 2 based on docker compose (using MySQL for persistence)
File descriptorfile description
Std:: map insert details
C language learning log 12.14
Case - simulated landlords (primary version)
C language learning log 1.16
redis
C language learning log 10.6
【多线程编程】Future接口获取线程执行结果数据
QT signal is automatically associated with the slot
Ruoyi cloud startup tutorial (hand-held graphics)
Course outline of market drawing 1- basic knowledge
Case -- the HashSet set stores the student object and traverses