forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8448a02
commit ec3a0eb
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
#include <unordered_set> | ||
using namespace std; | ||
|
||
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); | ||
class Solution | ||
{ | ||
public: | ||
int numUniqueEmails(vector<string>& emails) | ||
{ | ||
unordered_set<string> result; | ||
for (auto& email : emails) | ||
{ | ||
auto at_pos = email.find('@'); | ||
auto plus_pos = email.find('+'); | ||
if (plus_pos < at_pos) | ||
{ | ||
auto len = at_pos - plus_pos; | ||
email.erase(plus_pos, len); | ||
at_pos -= len; | ||
} | ||
auto dot_pos = email.find('.'); | ||
while (dot_pos < at_pos) | ||
{ | ||
email.erase(dot_pos, 1); | ||
at_pos--; | ||
dot_pos = email.find('.'); | ||
} | ||
result.insert(email); | ||
} | ||
return result.size(); | ||
} | ||
}; | ||
int main() | ||
{ | ||
vector<string> emails = {"[email protected]","[email protected]","[email protected]"}; | ||
cout << Solution().numUniqueEmails(emails); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution: | ||
def numUniqueEmails(self, emails): | ||
""" | ||
:type emails: List[str] | ||
:rtype: int | ||
""" | ||
raw_emails = set() | ||
for email in emails: | ||
sp_at = email.split('@') | ||
pre = sp_at[0].split('+') | ||
raw_emails.add(pre[0].replace('.', '') + sp_at[1]) | ||
|
||
return len(raw_emails) | ||
|
||
|
||
if __name__ == "__main__": | ||
emails = ["[email protected]","[email protected]","[email protected]"] | ||
print(Solution().numUniqueEmails(emails)) |