Skip to content

Commit 6d4ba7a

Browse files
committed
Add 2_1_fix
1 parent 3ce326c commit 6d4ba7a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

929/step2_1_fix.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
3+
*/
4+
class Solution {
5+
public:
6+
int numUniqueEmails(vector<string>& emails) {
7+
for (auto& email : emails) {
8+
canonicalize_email(email);
9+
}
10+
set<string> unique_emails(emails.begin(), emails.end());
11+
return unique_emails.size();
12+
}
13+
14+
private:
15+
void canonicalize_email(string& email) {
16+
auto at_pos = email.rfind('@');
17+
string local_part = email.substr(0, at_pos);
18+
auto plus_pos = local_part.find('+');
19+
string canonicalized_local_part = local_part.substr(0, plus_pos);
20+
std::erase(canonicalized_local_part, '.');
21+
string domain_part = email.substr(at_pos, email.size() - at_pos);
22+
email = canonicalized_local_part + domain_part;
23+
}
24+
};

0 commit comments

Comments
 (0)