개발에 AtoZ까지

[JAVA][Array] Unique Email Address 본문

코딩테스트 준비/기타문제

[JAVA][Array] Unique Email Address

AtoZ 개발자 2021. 2. 5. 07:48
반응형

1. 문제

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?

 

2. 문제해설

input으로 여러개의 이메일 주소가 주어지는데 실제로 전달받는 메일은 몇개인가?단, 아래 규칙을 따른다.1) '@'를 기준으로 앞에는 Local Name, 뒤는 Domain Name이다.2) Local Name에 '.' 있으면 무시한다. ex) d.e@naver.com == de@naver.com3) Local Name에 '+' 이 시작하는 부분부터 신경쓰지 않는다. ex) d.e+23@naver.com = de@naver.com

 

3. 코드포맷

public class UniqueEamilAddress {
	public static void main(String[] args) {
		String[] emails = { "test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com",
				"testemail+david@lee.tcode.com", "bcgrhji.dl@naver.com", "bcg+rhji.dl@naver.com",
				"bcgrhji.asddl@naver.com" };
		System.out.println(numUniqueEmailsCount(emails));
	}
}

 

4. 접근 방법

1) 위의 규칙을 적용하여 이메일 주소 확인

2) 확인된 이메일 주소를 저장할 변수 필요(단, 중복을 허용하지 않는 구조체 사용)

 

5. 코드

package Array;

import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class UniqueEamilAddress {
	public static void main(String[] args) {
		String[] emails = { "test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com",
				"testemail+david@lee.tcode.com", "bcgrhji.dl@naver.com", "bcg+rhji.dl@naver.com",
				"bcgrhji.asddl@naver.com" };
		System.out.println(numUniqueEmailsCount(emails));
	}

	private static int numUniqueEmailsCount(String[] emails) {
		Set<String> set = new HashSet<String>();
		
		for(String str:emails) {
			StringTokenizer st = new StringTokenizer(str,"@");
			String localName = st.nextToken();
			String domainName = st.nextToken();
			//문자열이 추가 또는 수정작업이 많을시에는 StringBuilder 또는 StringBuffer를 사용한다.  string 사용시에는 string은 불변속성으로 부적합함 
			StringBuilder sb = new StringBuilder();
			for(int i=0;i<localName.length();i++) {
				//+인 경우 중단한다.
				if(localName.charAt(i)=='+') break;
				//.인 경우는 반복문을 계속 진행한다.
				else if(localName.charAt(i)=='.') {
					continue;
				}
				//+,.이 아닌 경우, 문자열인 경우에는 sb에 저장한다.
				else {
					sb.append(localName.charAt(i));
				}
			}
			//문제는 전달받는 이메일의 개수를 물어본거기 때문에 @를 다시 붙이지 않았음
			sb.append(domainName);
			//set에 추가함, 만약 기존에 있던 이메일이라면 추가가 되지 않는다.
			set.add(sb.toString());
		}
		return set.size();
	}
}

 

반응형

'코딩테스트 준비 > 기타문제' 카테고리의 다른 글

[JAVA][Array] PlusOne  (0) 2021.02.01
[JAVA][Array] KClosestPointstoOrigin  (0) 2021.02.01
[JAVA][Array] LicenseKey Formatting  (0) 2021.01.27
[JAVA][Array] Jewels And Stones  (0) 2021.01.27
[JAVA][Array] MeetingRoom2  (0) 2021.01.26
[JAVA][Array] MergeInterval  (0) 2021.01.26
[JAVA][Array] Daily Temperature  (0) 2021.01.26
Comments