Skip to content

Commit d4ed7ed

Browse files
authored
Merge pull request #28 from colorbox/929
929. Unique Email Addresses
2 parents 3a4d16b + 6d4ba7a commit d4ed7ed

File tree

6 files changed

+163
-0
lines changed

6 files changed

+163
-0
lines changed

929/step1.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Solve Time: 25:00
3+
4+
Time Order: O(n)
5+
Space Order: O(n)
6+
7+
方針は早くに定まっていたものの、C++の文字列操作が思うようにいかずに時間がかかってしまった。
8+
9+
*/
10+
class Solution {
11+
public:
12+
int numUniqueEmails(vector<string>& emails) {
13+
set<string> unique_emails;
14+
for (const string& email : emails) {
15+
auto at_pos = email.find_first_of("@");
16+
string local_part = email.substr(0, at_pos);
17+
auto plus_pos = local_part.find_first_of("+");
18+
if (plus_pos == std::string::npos) {
19+
plus_pos = local_part.size();
20+
}
21+
string local_part_without_plus = local_part.substr(0, plus_pos);
22+
std::erase(local_part_without_plus, '.');
23+
email.replace(0, at_pos, local_part_without_plus);
24+
unique_emails.insert(email);
25+
}
26+
return unique_emails.size();
27+
}
28+
};

929/step2_1.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Time Order : O(n)
3+
Space Order : O(n)
4+
5+
step1の改良版
6+
コメント使用ででローカルパートに`@`が含むものを対応するために`rfind`を利用。
7+
メールアドレス正規化処理をメソッド抽出。
8+
*/
9+
class Solution {
10+
public:
11+
int numUniqueEmails(vector<string>& emails) {
12+
set<string> unique_emails;
13+
for (auto email : emails) {
14+
unique_emails.insert(canonicalize_email(email));
15+
}
16+
return unique_emails.size();
17+
}
18+
19+
private:
20+
string canonicalize_email(string email) {
21+
const auto at_pos = email.rfind("@");
22+
const string local_part = email.substr(0, at_pos);
23+
const auto plus_pos = local_part.find("+");
24+
string canonical_local_part = local_part.substr(0, plus_pos);
25+
std::erase(canonical_local_part, '.');
26+
email.replace(0, at_pos, canonical_local_part);
27+
return email;
28+
}
29+
};

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+
};

929/step2_2.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Time Order : O(n)
3+
Space Order : O(n)
4+
5+
ステートマシン的な解法
6+
*/
7+
class Solution {
8+
public:
9+
int numUniqueEmails(vector<string>& emails) {
10+
set<string> unique_emails;
11+
for (auto email : emails) {
12+
unique_emails.insert(canonicalize_email(email));
13+
}
14+
return unique_emails.size();
15+
}
16+
17+
private:
18+
string canonicalize_email(string& email) {
19+
string canonicalized_email = "";
20+
auto at_pos = email.rfind('@');
21+
string local_part = email.substr(0, at_pos);
22+
for (auto c : local_part) {
23+
if (c == '.') {
24+
continue;
25+
}
26+
if (c == '+') {
27+
break;
28+
}
29+
canonicalized_email += c;
30+
}
31+
return email.replace(0, at_pos, canonicalized_email);
32+
}
33+
};

929/step2_3.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Time Order : O(n)
3+
Space Order : O(n)
4+
5+
正規表現を使用したパターン
6+
local-partから`.`と`+`以降を削除する正規表現を使用している。
7+
*/
8+
class Solution {
9+
public:
10+
int numUniqueEmails(vector<string>& emails) {
11+
set<string> unique_emails;
12+
for (auto email : emails) {
13+
unique_emails.insert(canonicalize_email(email));
14+
}
15+
return unique_emails.size();
16+
}
17+
18+
private:
19+
string canonicalize_email(string& email) {
20+
auto at_pos = email.rfind('@');
21+
string local_part = email.substr(0, at_pos);
22+
std::regex dot_and_plus_suffix("\\.|\\+.*");
23+
local_part = std::regex_replace(local_part, dot_and_plus_suffix, "");
24+
25+
email.replace(0, at_pos, local_part);
26+
return email;
27+
}
28+
};

929/step3.cpp

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

0 commit comments

Comments
 (0)