Skip to content

Commit 1b31163

Browse files
committed
🚀 08-Sep-2020
1 parent 272703f commit 1b31163

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Given a pattern and a string str, find if str follows the same pattern.
2+
3+
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
4+
5+
Example 1:
6+
7+
Input: pattern = "abba", str = "dog cat cat dog"
8+
Output: true
9+
Example 2:
10+
11+
Input:pattern = "abba", str = "dog cat cat fish"
12+
Output: false
13+
Example 3:
14+
15+
Input: pattern = "aaaa", str = "dog cat cat dog"
16+
Output: false
17+
Example 4:
18+
19+
Input: pattern = "abba", str = "dog dog dog dog"
20+
Output: false
21+
Notes:
22+
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
// Two hashmap
36+
37+
class Solution {
38+
public:
39+
bool wordPattern(string pattern, string str) {
40+
vector<string> all;
41+
stringstream s(str);
42+
string tmp;
43+
while(s>>tmp){
44+
all.push_back(tmp);
45+
}
46+
47+
int plen=pattern.length();
48+
int alen=all.size();
49+
50+
if(plen!=alen) return false;
51+
52+
unordered_map<char, string> mp;
53+
set<string> unique;
54+
55+
for(int i=0;i<plen;i++){
56+
if(mp.find(pattern[i])==mp.end()){
57+
if(unique.find(all[i])!=unique.end())
58+
return false;
59+
unique.insert(all[i]);
60+
mp[pattern[i]]=all[i];
61+
62+
}
63+
else {
64+
if(mp[pattern[i]]!=all[i])
65+
return false;
66+
}
67+
}
68+
69+
return true;
70+
}
71+
};
72+
73+
74+
75+
76+
77+
78+
79+
80+
// One map
81+
82+
class Solution {
83+
public:
84+
bool wordPattern(string pattern, string str) {
85+
vector<string> all;
86+
stringstream s(str);
87+
string tmp;
88+
while(s>>tmp){
89+
all.push_back(tmp);
90+
}
91+
92+
int plen=pattern.length();
93+
int alen=all.size();
94+
95+
if(plen!=alen) return false;
96+
97+
unordered_map<string, int> mp;
98+
99+
for(int i=0;i<plen;i++){
100+
string ch="#"; // for pattern
101+
ch+=pattern[i];
102+
103+
if(mp.find(ch)==mp.end())
104+
mp[ch]=i;
105+
106+
if(mp.find(all[i])==mp.end())
107+
mp[all[i]]=i;
108+
109+
if(mp[ch]!=mp[all[i]])
110+
return false;
111+
}
112+
113+
return true;
114+
}
115+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Given a pattern and a string str, find if str follows the same pattern.
2+
3+
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
4+
5+
Example 1:
6+
7+
Input: pattern = "abba", str = "dog cat cat dog"
8+
Output: true
9+
Example 2:
10+
11+
Input:pattern = "abba", str = "dog cat cat fish"
12+
Output: false
13+
Example 3:
14+
15+
Input: pattern = "aaaa", str = "dog cat cat dog"
16+
Output: false
17+
Example 4:
18+
19+
Input: pattern = "abba", str = "dog dog dog dog"
20+
Output: false
21+
Notes:
22+
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
// Two hashmap
36+
37+
class Solution {
38+
public:
39+
bool wordPattern(string pattern, string str) {
40+
vector<string> all;
41+
stringstream s(str);
42+
string tmp;
43+
while(s>>tmp){
44+
all.push_back(tmp);
45+
}
46+
47+
int plen=pattern.length();
48+
int alen=all.size();
49+
50+
if(plen!=alen) return false;
51+
52+
unordered_map<char, string> mp;
53+
set<string> unique;
54+
55+
for(int i=0;i<plen;i++){
56+
if(mp.find(pattern[i])==mp.end()){
57+
if(unique.find(all[i])!=unique.end())
58+
return false;
59+
unique.insert(all[i]);
60+
mp[pattern[i]]=all[i];
61+
62+
}
63+
else {
64+
if(mp[pattern[i]]!=all[i])
65+
return false;
66+
}
67+
}
68+
69+
return true;
70+
}
71+
};
72+
73+
74+
75+
76+
77+
78+
79+
80+
// One map
81+
82+
class Solution {
83+
public:
84+
bool wordPattern(string pattern, string str) {
85+
vector<string> all;
86+
stringstream s(str);
87+
string tmp;
88+
while(s>>tmp){
89+
all.push_back(tmp);
90+
}
91+
92+
int plen=pattern.length();
93+
int alen=all.size();
94+
95+
if(plen!=alen) return false;
96+
97+
unordered_map<string, int> mp;
98+
99+
for(int i=0;i<plen;i++){
100+
string ch="#"; // for pattern
101+
ch+=pattern[i];
102+
103+
if(mp.find(ch)==mp.end())
104+
mp[ch]=i;
105+
106+
if(mp.find(all[i])==mp.end())
107+
mp[all[i]]=i;
108+
109+
if(mp[ch]!=mp[all[i]])
110+
return false;
111+
}
112+
113+
return true;
114+
}
115+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Given a collection of distinct integers, return all possible permutations.
2+
3+
Example:
4+
5+
Input: [1,2,3]
6+
Output:
7+
[
8+
[1,2,3],
9+
[1,3,2],
10+
[2,1,3],
11+
[2,3,1],
12+
[3,1,2],
13+
[3,2,1]
14+
]
15+
16+
17+
18+
19+
20+
21+
22+
class Solution {
23+
public:
24+
25+
void allPermute(vector<int> nums, int start, vector<vector<int> > &res){
26+
if(start==nums.size()){
27+
res.push_back(nums);
28+
} else {
29+
for(int i=start;i<nums.size();i++){
30+
swap(nums[start], nums[i]);
31+
allPermute(nums, start+1, res);
32+
}
33+
}
34+
}
35+
36+
vector<vector<int>> permute(vector<int>& nums) {
37+
vector<vector<int> > res;
38+
allPermute(nums, 0, res);
39+
return res;
40+
}
41+
};

0 commit comments

Comments
 (0)