929. Unique Email Addresses

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
string process(string str){
string local_name , domain_name;
int i = 0;
while(str[i]!='@'){
if(str[i]=='+') break;
else if(str[i]!='.') local_name += str[i];
i++;
}
while(str[i]!='@') i++;
i++;
while(i<str.size()) domain_name+=str[i++];

string ans = local_name + "@"+domain_name;
return ans;
}
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> s;
for(string strs:emails) s.insert(process(strs));
return s.size();

}
};

analysis

  • time complexity O(nm)
  • space complexity O(nm)