From ac3b56e59a97b711c47d1098dfe4c7f8c7e8fb8d Mon Sep 17 00:00:00 2001 From: hiepndd Date: Sun, 17 Mar 2019 16:11:31 +0700 Subject: [PATCH] contribute solution problem 0929 and add me to file README.md --- README.md | 8 ++--- src/0929.Unique-Email-Address/README.md | 32 +++++++++++++++++++ src/0929.Unique-Email-Address/Solution.go | 15 +++++++++ .../Solution_test.go | 14 ++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/0929.Unique-Email-Address/README.md create mode 100644 src/0929.Unique-Email-Address/Solution.go create mode 100644 src/0929.Unique-Email-Address/Solution_test.go diff --git a/README.md b/README.md index 1aec3884d..3e24c23c5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # 📝 awesome-golang-leetcode - LeetCode of algorithms with golang solution(updating:smiley:).

@@ -29,16 +28,15 @@ LeetCode of algorithms with golang solution(updating:smiley:).

- - ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)): -| [Kyle Liu
Kyle Liu ](https://kyle.link)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=kylesliu "Code") [📝](#blog-kylesliu "Blogposts") [🎨](#design-kylesliu "Design") [📖](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=kylesliu "Documentation") | [hapiman2
hapiman2](https://github.com/hapiman2)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=hapiman2 "Code") | [hapiman
hapiman](https://github.com/hapiman)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=hapiman "Code") | [sihg yu
sihg yu](https://github.com/sihgyu)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=sihgyu "Code") | [plusweiwei
plusweiwei](https://github.com/plusweiwei)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=plusweiwei "Code") | [Sandy
Sandy](https://openset.github.com)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=openset "Code") | +| [Kyle Liu
Kyle Liu ](https://kyle.link)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=kylesliu "Code") [📝](#blog-kylesliu "Blogposts") [🎨](#design-kylesliu "Design") [📖](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=kylesliu "Documentation") | [hapiman2
hapiman2](https://github.com/hapiman2)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=hapiman2 "Code") | [hapiman
hapiman](https://github.com/hapiman)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=hapiman "Code") | [sihg yu
sihg yu](https://github.com/sihgyu)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=sihgyu "Code") | [plusweiwei
plusweiwei](https://github.com/plusweiwei)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=plusweiwei "Code") | [Sandy
Sandy](https://openset.github.com)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=openset "Code") | [hiepndd
hiepndd](https://github.com/hiepndd)
[💻](https://github.com/kylesliu/awesome-golang-leetcode/commits?author=hiepndd "Code") | | :---: | :---: | :---: | :---: | :---: | :---: | + -This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! \ No newline at end of file +This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! diff --git a/src/0929.Unique-Email-Address/README.md b/src/0929.Unique-Email-Address/README.md new file mode 100644 index 000000000..5ea282fcb --- /dev/null +++ b/src/0929.Unique-Email-Address/README.md @@ -0,0 +1,32 @@ +# [929. Unique Email Addresses][title] + +## Description + +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? + +**Example:** + +``` +Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] +Output: 2 +Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails + +``` + +## NOTES + +1 <= emails[i].length <= 100 +1 <= emails.length <= 100 +Each emails[i] contains exactly one '@' character. diff --git a/src/0929.Unique-Email-Address/Solution.go b/src/0929.Unique-Email-Address/Solution.go new file mode 100644 index 000000000..13a6ebb17 --- /dev/null +++ b/src/0929.Unique-Email-Address/Solution.go @@ -0,0 +1,15 @@ +package Solution + +import ( + "strings" +) + +func numUniqueEmails(emails []string) int { + emailSet := map[string]bool{} + for _, email := range emails { + parts := strings.Split(email, "@") + localName := strings.Replace(strings.Split(parts[0], "+")[0], ".", "", -1) + emailSet[localName+"@"+parts[1]] = true + } + return len(emailSet) +} diff --git a/src/0929.Unique-Email-Address/Solution_test.go b/src/0929.Unique-Email-Address/Solution_test.go new file mode 100644 index 000000000..d2ae7266c --- /dev/null +++ b/src/0929.Unique-Email-Address/Solution_test.go @@ -0,0 +1,14 @@ +package Solution + +import ( + "testing" +) + +func TestSolution(t *testing.T) { + listTest := []string{"test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", "testemail+david@lee.tcode.com"} + got := numUniqueEmails(listTest) + want := 2 + if got != want { + t.Errorf("want '%d' but got '%d'", want, got) + } +}