LeetCode From Easy To Hard No10 [easy]: 이메일 주소 고유성

3419 단어 LeetCode
very email consists of a local name and a domain name, separated by the @ sign.
For example, in  [email protected]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,  "[email protected]"  and  "[email protected]"  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  [email protected]  will be forwarded to  [email protected] .  (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? 
 
Example 1:
Input: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" actually receive mails

 
Note:
  • 1 <= emails[i].length <= 100
  • 1 <= emails.length <= 100
  • Each  emails[i]  contains exactly one  '@'  character.
  • All local and domain names are non-empty.
  • Local names do not start with a  '+'  character.

  • 제목 분석: 이메일 address 그룹을 지정하고 몇 개의 다른 이메일 adress를 출력하는지, 이메일 adress는 "@"를 분할점으로 한다
    ① "@"왼쪽에 "."이 나타나면예를 들어 [email protected]사실[email protected];"+"가 나타나면 "+"에서 "@"사이의 내용은 모두 무시됩니다. 예를 들어 [email protected]+n사실[email protected]+n; 
    ② "@"오른쪽에 "만약 나타난다면.""+"와는 아무런 변화가 없을 것이다.
    Set 컬렉션을 사용하여 중복된 요소가 포함되지 않은 특성의 중량을 제거합니다.
    import java.util.HashSet;
    import java.util.Set;
    
    class Solution {
        public static int numUniqueEmails(String[] emails) {
            Set set = new HashSet(); //  set 
            for (String e : emails) {
                int atIndex = e.indexOf("@"); //  @ 
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < atIndex; i++) { //  @ 
                    if (e.charAt(i) == '.') continue; //  “.” 
                    if (e.charAt(i) == '+') { //  “+” 
                        break;
                    } else {
                        sb.append(e.charAt(i));
                    }
                }
                set.add(sb.append("@").append(e.substring(atIndex)).toString()); //  “@” “@” , Set 。
            }
            return set.size();
        }
    
        public static void main(String[] args) {
            String[] emails = {"[email protected]", "[email protected]"};
            System.out.println(new Solution().numUniqueEmails(emails));
        }
    }

     

    좋은 웹페이지 즐겨찾기