forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
57 lines (42 loc) · 1.48 KB
/
main.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
/// 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
/// 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<pair<string, string>> similarity;
for(pair<string, string> p: pairs) {
similarity.insert(make_pair(p.first, p.second));
similarity.insert(make_pair(p.second, p.first));
}
for(int i = 0 ; i < words1.size() ; i ++)
if(words1[i] != words2[i] && similarity.find(make_pair(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;
}