> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/algorithm-problems/unique-email-address.md).

# 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 name**part 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?&#x20;

![](/files/-MNqH_tcbmJjS1kTX5uQ)

알고리즘\
1\. Input String 배열의 이메일리스트에 있는 이메일들을 로컬네임과 도메인 네임 나눠서 처리한다.\
&#x20;  로컬네임 필터링 - '.' 나왔을 때 : continue\
&#x20;                            \- '+' 나왔을 때 : break\
&#x20;                            \- '@' 나왔을 때 : break\
2\. 1에서 처리한 로컬네임과 도메인 네임을 합쳐서 Set에 저장한다.\
3\. Set의 크기를 리턴한다.

알고리즘을 Java로 구현

```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 하나하나를 뽑아서 저장하기 위해&#x20;

```java
...
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();
 }
    
```
