-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUniqueEmailAddresses.java
40 lines (34 loc) · 1.1 KB
/
UniqueEmailAddresses.java
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
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.HashSet;
import java.util.Set;
// https://leetcode.com/problems/unique-email-addresses/
public class UniqueEmailAddresses {
private final String[] input;
public UniqueEmailAddresses(String[] input) {
this.input = input;
}
public int solution() {
Set<String> uniq = new HashSet<>();
for (String email : input) {
StringBuilder parsed = new StringBuilder();
boolean ignore = false;
boolean domain = false;
for (int i = 0; i < email.length(); i++) {
char c = email.charAt(i);
if (c != '.' || domain) {
if (c == '+') {
ignore = true;
} else if (c == '@') {
ignore = false;
domain = true;
}
if (!ignore) {
parsed.append(c);
}
}
}
uniq.add(parsed.toString());
}
return uniq.size();
}
}