Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 529aa83

Browse files
feat: add solutions to lc problem: No.0929 (doocs#3857)
No.0929.Unique Email Addresses
1 parent 47324f6 commit 529aa83

File tree

9 files changed

+321
-248
lines changed

9 files changed

+321
-248
lines changed

‎solution/0900-0999/0929.Unique Email Addresses/README.md‎

Lines changed: 107 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ tags:
7979

8080
### 方法一:哈希表
8181

82-
利用哈希表存放转换后的电子邮件,最后返回哈希表的 size 即可。
82+
我们可以用一个哈希表 $s$ 来存储所有的电子邮件地址,然后遍历数组 $\textit{emails},ドル对于每个电子邮件地址,我们将其分为本地名和域名两部分,然后对本地名进行处理,去掉所有的点号和加号后面的字符,最后将处理后的本地名和域名拼接起来,加入哈希表 $s$ 中。
83+
84+
最后返回哈希表 $s$ 的大小即可。
85+
86+
时间复杂度 $O(L),ドル空间复杂度 $O(L),ドル其中 $L$ 为所有电子邮件地址的长度之和。
8387

8488
<!-- tabs:start -->
8589

@@ -90,11 +94,15 @@ class Solution:
9094
def numUniqueEmails(self, emails: List[str]) -> int:
9195
s = set()
9296
for email in emails:
93-
local, domain = email.split('@')
94-
local = local.replace('.', '')
95-
if (i := local.find('+')) != -1:
96-
local = local[:i]
97-
s.add(local + '@' + domain)
97+
local, domain = email.split("@")
98+
t = []
99+
for c in local:
100+
if c == ".":
101+
continue
102+
if c == "+":
103+
break
104+
t.append(c)
105+
s.add("".join(t) + "@" + domain)
98106
return len(s)
99107
```
100108

@@ -105,14 +113,20 @@ class Solution {
105113
public int numUniqueEmails(String[] emails) {
106114
Set<String> s = new HashSet<>();
107115
for (String email : emails) {
108-
String[] t = email.split("@");
109-
String local = t[0].replace(".", "");
110-
String domain = t[1];
111-
int i = local.indexOf('+');
112-
if (i != -1) {
113-
local = local.substring(0, i);
116+
String[] parts = email.split("@");
117+
String local = parts[0];
118+
String domain = parts[1];
119+
StringBuilder t = new StringBuilder();
120+
for (char c : local.toCharArray()) {
121+
if (c == '.') {
122+
continue;
123+
}
124+
if (c == '+') {
125+
break;
126+
}
127+
t.append(c);
114128
}
115-
s.add(local + "@" + domain);
129+
s.add(t.toString() + "@" + domain);
116130
}
117131
return s.size();
118132
}
@@ -126,15 +140,21 @@ class Solution {
126140
public:
127141
int numUniqueEmails(vector<string>& emails) {
128142
unordered_set<string> s;
129-
for (auto& email : emails) {
130-
int i = email.find('@');
131-
string local = email.substr(0, i);
132-
string domain = email.substr(i + 1);
133-
i = local.find('+', 0);
134-
if (~i) local = local.substr(0, i);
135-
while (~(i = local.find('.', 0)))
136-
local.erase(local.begin() + i);
137-
s.insert(local + "@" + domain);
143+
for (const string& email : emails) {
144+
size_t atPos = email.find('@');
145+
string local = email.substr(0, atPos);
146+
string domain = email.substr(atPos + 1);
147+
string t;
148+
for (char c : local) {
149+
if (c == '.') {
150+
continue;
151+
}
152+
if (c == '+') {
153+
break;
154+
}
155+
t.push_back(c);
156+
}
157+
s.insert(t + "@" + domain);
138158
}
139159
return s.size();
140160
}
@@ -145,13 +165,22 @@ public:
145165
146166
```go
147167
func numUniqueEmails(emails []string) int {
148-
s := map[string]bool{}
168+
s := make(map[string]struct{})
149169
for _, email := range emails {
150-
i := strings.IndexByte(email, '@')
151-
local := strings.SplitN(email[:i], "+", 2)[0]
152-
local = strings.ReplaceAll(local, ".", "")
153-
domain := email[i:]
154-
s[local+domain] = true
170+
parts := strings.Split(email, "@")
171+
local := parts[0]
172+
domain := parts[1]
173+
var t strings.Builder
174+
for _, c := range local {
175+
if c == '.' {
176+
continue
177+
}
178+
if c == '+' {
179+
break
180+
}
181+
t.WriteByte(byte(c))
182+
}
183+
s[t.String()+"@"+domain] = struct{}{}
155184
}
156185
return len(s)
157186
}
@@ -161,84 +190,80 @@ func numUniqueEmails(emails []string) int {
161190

162191
```ts
163192
function numUniqueEmails(emails: string[]): number {
164-
return new Set(
165-
emails
166-
.map(email => email.split('@'))
167-
.map(([start, end]) => start.replace(/\+.*|\./g, '') + '@' + end),
168-
).size;
193+
const s = new Set<string>();
194+
for (const email of emails) {
195+
const [local, domain] = email.split('@');
196+
let t = '';
197+
for (const c of local) {
198+
if (c === '.') {
199+
continue;
200+
}
201+
if (c === '+') {
202+
break;
203+
}
204+
t += c;
205+
}
206+
s.add(t + '@' + domain);
207+
}
208+
return s.size;
169209
}
170210
```
171211

172212
#### Rust
173213

174214
```rust
175215
use std::collections::HashSet;
216+
176217
impl Solution {
177218
pub fn num_unique_emails(emails: Vec<String>) -> i32 {
178-
let mut set = HashSet::new();
179-
for email in emails.iter() {
180-
let res: Vec<&str> = email.split('@').collect();
181-
let mut s = String::new();
182-
for &c in res[0].as_bytes().iter() {
183-
if c == b'.' {
219+
let mut s = HashSet::new();
220+
221+
for email in emails {
222+
let parts: Vec<&str> = email.split('@').collect();
223+
let local = parts[0];
224+
let domain = parts[1];
225+
let mut t = String::new();
226+
for c in local.chars() {
227+
if c == '.' {
184228
continue;
185229
}
186-
if c == b'+' {
230+
if c == '+' {
187231
break;
188232
}
189-
s.push(caschar);
233+
t.push(c);
190234
}
191-
s.push('@');
192-
s.push_str(res[1]);
193-
set.insert(s);
235+
s.insert(format!("{}@{}", t, domain));
194236
}
195-
set.len() as i32
237+
238+
s.len() as i32
196239
}
197240
}
198241
```
199242

200243
#### JavaScript
201244

202245
```js
203-
const numUniqueEmails2 = function (emails) {
204-
const emailFilter = function (str) {
205-
let index = str.search(/@/);
206-
let s = str.substring(0, index);
207-
let s2 = str.substring(index + 1, str.length);
208-
let res = '';
209-
for (let i = 0; i < s.length; i++) {
210-
if (s[i] === '+') break;
211-
if (s[i] === '.') continue;
212-
res = res + s[i];
213-
}
214-
return res + s2;
215-
};
216-
217-
let arr = [];
218-
for (let i = 0; i < emails.length; i++) {
219-
let t = emailFilter(emails[i]);
220-
if (arr.indexOf(t) === -1) {
221-
arr.push(t);
246+
/**
247+
* @param {string[]} emails
248+
* @return {number}
249+
*/
250+
var numUniqueEmails = function (emails) {
251+
const s = new Set();
252+
for (const email of emails) {
253+
const [local, domain] = email.split('@');
254+
let t = '';
255+
for (const c of local) {
256+
if (c === '.') {
257+
continue;
258+
}
259+
if (c === '+') {
260+
break;
261+
}
262+
t += c;
222263
}
264+
s.add(t + '@' + domain);
223265
}
224-
return arr.length;
225-
};
226-
227-
const numUniqueEmails = function (emails) {
228-
let arr = emails.map(str => {
229-
let index = str.search(/@/);
230-
let s = str.substring(0, index);
231-
let s2 = str.substring(index + 1, str.length);
232-
let res = '';
233-
for (let i = 0; i < s.length; i++) {
234-
if (s[i] === '+') break;
235-
if (s[i] === '.') continue;
236-
res = res + s[i];
237-
}
238-
return res + s2;
239-
});
240-
let set = new Set(arr);
241-
return set.size;
266+
return s.size;
242267
};
243268
```
244269

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /