【原題目】:

Every email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.comalice 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? 

 

參考 : https://leetcode.com/problems/unique-email-addresses/

 

【My Solution】:

solution.PNG

 

【學習重點】

1. 在這裡我使用了 Set 這個 collection 容器來儲存我最後的結果,如果我們採用 List 來儲存結果答案則會錯

因為在這邊有一個重點是,Set 中的元素不能重複出現,而List 可以有重複元素。

2. String 的用法 :

   (1) 

int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.

  (2)

String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.

 (3)

boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.

 (4)

String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with
the given replacement.

這邊需要特別注意的是,replaceAll 接受 regular expression ,而 '.' 這個字元在 regex 代表的是任何字元,

因此我們要取代所有的 '.' 為 ' ' 時,必須使用'\\.'

參考 : https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

arrow
arrow

    Mark Zhang 發表在 痞客邦 留言(0) 人氣()