File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(n * l)
2
+ // Space: O(1)
3
+
4
+ // freq table
5
+ class Solution {
6
+ public:
7
+ vector<string> removeAnagrams (vector<string>& words) {
8
+ vector<string> result;
9
+ vector<int > prev;
10
+ for (const auto & x : words) {
11
+ vector<int > cnt (26 );
12
+ for (const auto & c : x) {
13
+ ++cnt[c - ' a' ];
14
+ }
15
+ if (!empty (prev) && prev == cnt) {
16
+ continue ;
17
+ }
18
+ prev = move (cnt);
19
+ result.emplace_back (x);
20
+ }
21
+ return result;
22
+ }
23
+ };
24
+
25
+ // Time: O(n * llogl)
26
+ // Space: O(l)
27
+ // sort
28
+ class Solution2 {
29
+ public:
30
+ vector<string> removeAnagrams (vector<string>& words) {
31
+ vector<string> result;
32
+ string prev;
33
+ for (const auto & x : words) {
34
+ string s (x);
35
+ sort (begin (s), end (s));
36
+ if (!empty (prev) && prev == s) {
37
+ continue ;
38
+ }
39
+ prev = move (s);
40
+ result.emplace_back (x);
41
+ }
42
+ return result;
43
+ }
44
+ };
You can’t perform that action at this time.
0 commit comments