forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
59 lines (44 loc) · 1.54 KB
/
main2.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
58
59
/// Source : https://leetcode.com/problems/sentence-similarity/solution/
/// Author : liuyubobobo
/// Time : 2017-11-25
#include <iostream>
#include <vector>
#include <set>
using namespace std;
/// Using Set
/// Saving Pairs String
/// Time Complexity: O(len(pairs) + len(s))
/// Space Complexity: O(len(pairs))
class Solution {
public:
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
if(words1.size() != words2.size())
return false;
if(words1.size() == 0)
return true;
set<string> similarity;
for(pair<string, string> p: pairs) {
string hashcode1 = p.first + "#" + p.second;
string hashcode2 = p.second + "#" + p.first;
similarity.insert(hashcode1);
similarity.insert(hashcode2);
}
for(int i = 0 ; i < words1.size() ; i ++)
if(words1[i] != words2[i] && similarity.find(words1[i] + "#" + words2[i]) == similarity.end())
return false;
return true;
}
};
void printBool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<string> words1 = {"great", "acting", "skills"};
vector<string> words2 = {"fine", "drama", "talent"};
vector<pair<string, string>> pairs;
pairs.push_back(make_pair("great", "fine"));
pairs.push_back(make_pair("acting", "drama"));
pairs.push_back(make_pair("skills", "talent"));
printBool(Solution().areSentencesSimilar(words1, words2, pairs));
return 0;
}