Unique Email Address
Every email consists of a local name and a domain name, separated by the @ sign.
For example, in alice@leetcode.com
, alice
is the local name, and leetcode.com
is the domain name.
Besides lowercase letters, these emails may contain '.'
s or '+'
s.
If you add periods ('.'
) between some characters in the local namepart of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, "alice.z@leetcode.com"
and "alicez@leetcode.com"
forward to the same email address. (Note that this rule does not apply for domain names.)
If you add a plus ('+'
) in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com
will be forwarded to my@email.com
. (Again, this rule does not apply for domain names.)
It is possible to use both of these rules at the same time.
Given a list of emails
, we send one email to each address in the list. How many different addresses actually receive mails?

์๊ณ ๋ฆฌ์ฆ 1. Input String ๋ฐฐ์ด์ ์ด๋ฉ์ผ๋ฆฌ์คํธ์ ์๋ ์ด๋ฉ์ผ๋ค์ ๋ก์ปฌ๋ค์๊ณผ ๋๋ฉ์ธ ๋ค์ ๋๋ ์ ์ฒ๋ฆฌํ๋ค. ๋ก์ปฌ๋ค์ ํํฐ๋ง - '.' ๋์์ ๋ : continue - '+' ๋์์ ๋ : break - '@' ๋์์ ๋ : break 2. 1์์ ์ฒ๋ฆฌํ ๋ก์ปฌ๋ค์๊ณผ ๋๋ฉ์ธ ๋ค์์ ํฉ์ณ์ Set์ ์ ์ฅํ๋ค. 3. Set์ ํฌ๊ธฐ๋ฅผ ๋ฆฌํดํ๋ค.
์๊ณ ๋ฆฌ์ฆ์ Java๋ก ๊ตฌํ
package java_basic;
import java.util.*;
public class UniqueEmail {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] emails = {"test.email+alex@leetcode.com",
"test.e.mail+bob.cathy@leetcode.com",
"testemail+david@lee.tcode.com"};
System.out.println(numUniqueEmails(emails));
}
public static int numUniqueEmails(String[] emails) {
//0. ๊ทธ๋ฆ ์์ฑ
Set<String> uniqueEmails = new HashSet<String>();
//1. String ๋ฐฐ์ด(emails) ์์ ๊ฐ๊ฐ์ String(๊ฐ๊ฐ์ ์ด๋ฉ์ผ) ๋ก์ปฌ๋ค์๊ณผ ๋๋ฉ์ธ ๋ค์์ ๋๋ ์ ๊ตฌํ๋ค.
//2. 1์ ํฉ์ณ์ Set์ ๋ฃ๋๋ค.
for(String email : emails) {
String localName = getLocalname(email);//1
String domainName = getDomainname(email);//1
uniqueEmails.add(localName + '@' + domainName);//2
}
//3. Set์ ํฌ๊ธฐ๋ฅผ ๋ฆฌํด(์ด๋ฉ์ผ ์ฃผ์๋ ์ค๋ณตX,์์์๊ดX)
return uniqueEmails.size();
}
private static String getLocalname (String email) {
//1. ์๋ก์ด String ์์ฑ(StringBuilder)
StringBuilder sb = new StringBuilder();
//2. emails์ ์ฒ ์ํ๋ํ๋๋ฅผ ๋ค ๋ฝ์๋ด์ ์๋ก์ด String์ ์ ์ฅํ๋ค.
//String์ char๋ก ์ด๋ฃจ์ด์ ธ์๊ธฐ์, charAt(i) ๋ฐ์ดํฐ๋ char์ด๋ค!
//char->String->StringBuilder์ ์ ์ฅ!
for(int i = 0; i<email.length();i++) {
//๊ท์น๋ค๊ณผ @ ๋ง๋ฌ์ ๋ ๋จผ์ ํํฐ๋งํ ๋ค์์ String ์ ์ฅ ์ฒ๋ฆฌ!
if (email.charAt(i) == '.') {
continue;
}
if (email.charAt(i) == '+') {
break;
}
if (email.charAt(i) == '@') {
break;
}
//String ์ธํ ์ฒ ์ ํ๋ํ๋ = char์ด๋ค! ๊ทธ๋ฌ๋ฏ๋ก
//(ํต์ฌ0char->String->StringBuilder ์ ์ ์ฅ!
String str = String.valueOf(email.charAt(i));//char๋ฅผ String์ผ๋ก ํ๋ณํ!
sb.append(str);
}
return sb.toString();//StringBuilder๋ฅผ String์ผ๋ก ๋ฆฌํด!
}
private static String getDomainname (String email) {
return email.substring(email.indexOf('@')+1);
}
}
๋ฐฐ์ด ๋ด์ฉ ์ ๋ฆฌ (Java ๋ฌธ๋ฒ ํ ํฌ๋) 1. ์ด๋ฉ์ผ ์ฃผ์๋ String์ด๋ค. char๋ก ๊ตฌ์ฑ๋ String์ char ํ๋ํ๋๋ฅผ ๋ฝ์์ ์ ์ฅํ๊ธฐ ์ํด
...
for(int i=0; i<email.length();i++) {
//charAt(i)๋ก ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ char ํ์
์ด๋ค!
char c = email.charAt(i);
//์์ c ๋ฐ์ดํฐ๋ฅผ String์ผ๋ก ๋ณํํ๋ค!
String str = str.valueOf(c);
//๋ณํํ str์ ๋น๋ก์ StringBuilder์ ๋ฃ๋๋ค!
StringBuilder sb = new StringBuilder();
sb.append(str);
}
//๋ง๋ฌด๋ฆฌ ๋ฆฌํด ๊ฐ์ toString์ผ๋ก ๊น๋ํ๊ฒ String์ผ๋ก ํ๋ณํํด์ ๋ฆฌํด!
return sb.toString();
}
Last updated
Was this helpful?