当前位置:网站首页>890. Find and Replace Pattern
890. Find and Replace Pattern
2022-06-13 05:18: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.
source : Power button (LeetCode)
link :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.
source : Power button (LeetCode)
link :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
边栏推荐
- Install harbor (online offline)
- Enhanced for loop
- What is the saturate operation in opencv
- Error: unmapped character encoding GBK
- Basic operations of MySQL auto correlation query
- 17.6 unique_lock详解
- C language learning log 2.6
- Detailed explanation of R language sparse matrix
- Nonstandard compiler attribute extension
- Case - the list set stores student objects and traverses them in three ways
猜你喜欢
Mysql database crud operation
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
Basic operations of MySQL auto correlation query
Qmessagebox in pyqt5
Case - grade sorting - TreeSet set storage
Shell built-in string substitution
metaRTC4.0稳定版发布
Course outline of market drawing 1- basic knowledge
QT direction key to move focus
About Evaluation Metrics
随机推荐
Understanding of speech signal framing
File descriptorfile description
MySQL log management and master-slave replication
动态规划-最长公共子串
Case - simulated landlords (upgraded version)
Dup2 use
Std:: Map initialization
语音信号分帧的理解
Pyqt5 module
Anaconda configuring the mirror source
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Clause 33: decltype is used for auto & type formal parameters, with std:: forward
Dynamic and static libraries
C language learning log 10.8
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
[multi thread programming] the future interface obtains thread execution result data
Install harbor (online offline)
QT interface rendering style
Solutions to conflicts between xampp and VMware port 443
MySQL installation, architecture and management