forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
52 lines (41 loc) · 1.12 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
/// Source : https://leetcode.com/problems/uncommon-words-from-two-sentences/description/
/// Author : liuyubobobo
/// Time : 2018-08-12
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/// Uncommon words only occur once
/// Using just one HashMap
///
/// Time Complexity: O(len(A) + len(B))
/// Space Complexity: O(len(A) + len(B))
class Solution {
public:
vector<string> uncommonFromSentences(string A, string B) {
unordered_map<string, int> freq;
getFreq(freq, A);
getFreq(freq, B);
vector<string> res;
for(const pair<string, int>& p: freq)
if(p.second == 1)
res.push_back(p.first);
return res;
}
private:
void getFreq(unordered_map<string, int>& freq, const string& s){
int start = 0;
for(int i = start + 1; i <= s.size(); )
if(i == s.size() || s[i] == ' '){
freq[s.substr(start, i - start)] ++;
start = i + 1;
i = start + 1;
}
else
i ++;
return;
}
};
int main() {
return 0;
}