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?