본문 바로가기

Java/Java 알고리즘 LeetCode

[LeetCode- Ch.1 문자열] 1. 고유한 이메일 (+ StringBuilder, toCharArray)

반응형

https://leetcode.com/problems/unique-email-addresses/submissions/

 

Unique Email Addresses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

import java.util.*;
class Solution {
    public int numUniqueEmails(String[] emails) {
        int N=emails.length;
            Set<String> set = new HashSet<>();


            //2. Loop
            //: 로컬 이름 부분과 도메인 이름 부분으로 나눈다.
            for(int i=0;i<N;i++){
                //StringBuilder sb = new StringBuilder();

                //특정 문자 기준 분할 : split()
                String[] Name = emails[i].split("@");
                String LocalName=Name[0];
                String DomainName=Name[1];

                //로컬이름부
                StringBuilder sb = new StringBuilder();
                for(char x : LocalName.toCharArray()){

                    if(x=='+') {
                        break;
                    }
                    else if(x=='.'){
                        continue;
                    }
                    else{
                        sb.append(x);
                    }
                }
                LocalName=sb.toString();
                String answer=LocalName+"@"+DomainName;
                set.add(answer);
            }
            return set.size();
    }
}

 

 


class Solution {
    public int numUniqueEmails(String[] emails) {
        //이메일 : 로컬이름과 도메인이름으로 구성
        //소문자, '.', '+'가 포함될 수 있다.
        
        //로컬 이름 규칙
        //'.'의 경우 제거하며, '+'의 경우 이후 문자들이 무시된다.
        
        //동일한 이메일을 제거해야하므로 set을 사용한다.
        Set<String> set = new HashSet<>();
        
        for(String s : emails){
            String[] tmp =s.split("@");
            String localName=tmp[0];
            String domainName=tmp[1];
            
            String newLocal="";
            for(char c:localName.toCharArray()){
                if(c=='.') continue;
                if(c=='+') break;
                else newLocal+=c;
            }
            set.add(newLocal+"@"+domainName);
        }
        return set.size();
    }
}

 

-> 새로운 String을 만드는 것보다 StringBuilder를 이용하면 효율성을 높일 수 있다.

 

class Solution {
    public int numUniqueEmails(String[] emails) {
        //이메일 : 로컬이름과 도메인이름으로 구성
        //소문자, '.', '+'가 포함될 수 있다.
        
        //로컬 이름 규칙
        //'.'의 경우 제거하며, '+'의 경우 이후 문자들이 무시된다.
        
        //동일한 이메일을 제거해야하므로 set을 사용한다.
        Set<String> set = new HashSet<>();
        
        for(String s : emails){
            String[] tmp =s.split("@");
            String localName=tmp[0];
            String domainName=tmp[1];
            
            StringBuilder sb = new StringBuilder();
            for(char c:localName.toCharArray()){
                if(c=='.') continue;
                if(c=='+') break;
                else sb.append(c);
            }
            set.add(sb.toString()+"@"+domainName);
        }
        return set.size();
    }
}
반응형