-
Notifications
You must be signed in to change notification settings - Fork 160
/
Delete repeating strings.cpp
85 lines (58 loc) · 1.97 KB
/
Delete repeating strings.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
given input as an array of strings.
Such as: {"apple Orange", "ORANGE apple", "APPLe oRange", "HI There", "THERE
hI"};
return an array of strings. in the above case, will return "APPLe, oRange",
""THERE hI".
Here are the rules:
1. two strings are the same when words matches(case insensitive) and order
doesn't matter, so "apple Orange" == "APPLe oRange" == "ORANGE apple".
2. if multiple identical strings exist, only return the one that occurs at
the last location, so "APPLe oRange" and "THERE hI" will in the result.
3. the relative order cannot be changed, so we cannot have result as "THERE
hI", "APPLe oRange".
*/
/*
solution: use a set, the key is the ordered lowercase letter of the string. Scan from the end of original
array strings, if the set does not include the key, insert it to the set and to the final vector.
reverse the final vector at last.
O(n*len) time, O(n) space, n is the length of string array, len is the average length of each string.
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cctype>
#include<unordered_set>
using namespace std;
string GetKey(string str) {
string temp = str;
for (int i = 0; i < str.size(); ++i) {
if (isupper(temp[i])){
temp[i] = tolower(temp[i]);
}
}
sort(temp.begin(), temp.end());
return temp;
}
vector<string> DelReapting(const string str[], int len, unordered_set<string> &mp ) {
vector<string> res;
for(int i = len - 1; i >= 0; --i) {
string key = GetKey(str[i]);
if (mp.find(key) == mp.end()) {
mp.insert(key);
res.push_back(str[i]);
}
}
reverse(res.begin(), res.end());
return res;
}
int main() {
string str[5] = {"apple Orange", "ORANGE apple", "APPLe oRange", "HI There", "THERE hI"};
unordered_set<string> mp;
vector<string> result = DelReapting(str, 5, mp);
for (int i = 0; i < result.size(); ++i) {
cout<<result[i]<<endl;
}
return 0;
}