forked from yiqiao-yin/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique-email-addres.py
More file actions
70 lines (54 loc) · 1.86 KB
/
unique-email-addres.py
File metadata and controls
70 lines (54 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#https://leetcode.com/problems/unique-email-addresses/
"""
[0]
First we use set to store the processed email
set is implement in hashmap, so it takes O(1) to store unique value.
[1]
Split the email by '@' to local_name and domain_name
[2]
we find the first '+' and remove the char after it.
[3]
we remove the '.' in the string.
we iterate the local_name part 3 times
first time we are finding the first '@'
second time we are finding the first '+'
third time we are finding all the '.'
isn't faster to just iterate once?
so I come up with the second solution which only iterate once to process the email
but it is slower, I guess Python really do a good job on the string function
because they are implemented by C.
in other language, the second solution might be faster.
both solution are O(N)
"""
class Solution(object):
def numUniqueEmails(self, emails):
book = set() #[0]
for s in emails:
at = s.find('@') #[1]
local_name = s[:at]
domain_name = s[at:]
plus = local_name.find('+') #[2]
if plus!=-1: local_name = local_name[:plus]
local_name = local_name.replace('.', '') #[3]
r = local_name+domain_name #[0]
book.add(r)
return len(book)
class Solution(object):
def numUniqueEmails(self, emails):
book = set()
for email in emails:
r = ''
ignore = False
for i, c in enumerate(email):
if c=='@':
r+=email[i:]
break
if ignore: continue
if c=='.':
continue
elif c=='+':
ignore = True
else:
r+=c
book.add(r)
return len(book)