forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind and Replace Pattern.cpp
57 lines (48 loc) · 1.81 KB
/
Find and Replace Pattern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//😉If you Like the repository don't foget to star & fork the repository😉
class Solution {
public:
vector<int> found_Pattern(string pattern)
{
// if string is empty return empty vector.
if(pattern.size() == 0)
return {};
vector<int> v;
// ind variable for keeping track of unique characters
int ind = 0;
unordered_map<char,int> mp;
for(int i = 0; i<pattern.size(); ++i)
{
// if character not present in map, insert the char with an index,
// increment index because for next unique character the index should be differnt.
if(mp.find(pattern[i]) == mp.end())
{
mp.insert({pattern[i],ind++});
// also push the index to v(numeric pattern vector)
// mp[pattern[i]] gives the key to that character, here key is ind(which we are giving to every unique character)
v.push_back(mp[pattern[i]]);
}
else
{
// if char is already in map than push index mapped to that character into the vector
v.push_back(mp[pattern[i]]);
}
}
// return numeric pattern
return v;
}
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
// store numeric patten of Pattern string in v
vector<int> v = found_Pattern(pattern);
int n = words.size();
vector<string> ans;
// iterating and comparing pattern of each word with v(numeric pattern of Pattern)
for(int i = 0; i<n; ++i)
{
vector<int> pattern_word = found_Pattern(words[i]);
// if matched add words[i] to answer vector
if(v == pattern_word)
ans.push_back(words[i]);
}
return ans;
}
};