website/content.en/ChapterFour/0700~0799/0721.Accounts-Merge.md
Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emailsrepresenting emails of the account.
Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.
Example 1:
Input:
accounts = [["John", "[email protected]", "[email protected]"], ["John", "[email protected]"], ["John", "[email protected]", "[email protected]"], ["Mary", "[email protected]"]]
Output: [["John", '[email protected]', '[email protected]', '[email protected]'], ["John", "[email protected]"], ["Mary", "[email protected]"]]
Explanation:
The first and third John's are the same person as they have the common email "[email protected]".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', '[email protected]'], ['John', '[email protected]'],
['John', '[email protected]', '[email protected]', '[email protected]']] would still be accepted.
Note:
accounts will be in the range [1, 1000].accounts[i] will be in the range [1, 10].accounts[i][j] will be in the range [1, 30].Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is the name, and the remaining elements are emails representing the email addresses of the account. Now, we want to merge these accounts. If two accounts have some common email address, then the two accounts definitely belong to the same person. Note that even if two accounts have the same name, they may belong to different people because people may have the same name. A person can initially have any number of accounts, but all of their accounts have the same name. After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the remaining elements are email addresses arranged in order. accounts itself can be returned in any order.
Note:
map. Then use the union() operation of Union-Find to merge these numbers. Finally, concatenate the person's number with the corresponding email numbers.
package leetcode
import (
"sort"
"github.com/halfrost/leetcode-go/template"
)
// Solution 1: optimized search solution with Union-Find
func accountsMerge(accounts [][]string) (r [][]string) {
uf := template.UnionFind{}
uf.Init(len(accounts))
// emailToID separates all email addresses and maps them to id (array index)
// idToName maps id (array index) to name
// idToEmails maps id (array index) to the processed and deduplicated email group
emailToID, idToName, idToEmails, res := make(map[string]int), make(map[int]string), make(map[int][]string), [][]string{}
for id, acc := range accounts {
idToName[id] = acc[0]
for i := 1; i < len(acc); i++ {
pid, ok := emailToID[acc[i]]
if ok {
uf.Union(id, pid)
}
emailToID[acc[i]] = id
}
}
for email, id := range emailToID {
pid := uf.Find(id)
idToEmails[pid] = append(idToEmails[pid], email)
}
for id, emails := range idToEmails {
name := idToName[id]
sort.Strings(emails)
res = append(res, append([]string{name}, emails...))
}
return res
}
// Solution 2: brute-force solution with Union-Find
func accountsMerge1(accounts [][]string) [][]string {
if len(accounts) == 0 {
return [][]string{}
}
uf, res, visited := template.UnionFind{}, [][]string{}, map[int]bool{}
uf.Init(len(accounts))
for i := 0; i < len(accounts); i++ {
for j := i + 1; j < len(accounts); j++ {
if accounts[i][0] == accounts[j][0] {
tmpA, tmpB, flag := accounts[i][1:], accounts[j][1:], false
for j := 0; j < len(tmpA); j++ {
for k := 0; k < len(tmpB); k++ {
if tmpA[j] == tmpB[k] {
flag = true
break
}
}
if flag {
break
}
}
if flag {
uf.Union(i, j)
}
}
}
}
for i := 0; i < len(accounts); i++ {
if visited[i] {
continue
}
emails, account, tmpMap := accounts[i][1:], []string{accounts[i][0]}, map[string]string{}
for j := i + 1; j < len(accounts); j++ {
if uf.Find(j) == uf.Find(i) {
visited[j] = true
for _, v := range accounts[j][1:] {
tmpMap[v] = v
}
}
}
for _, v := range emails {
tmpMap[v] = v
}
emails = []string{}
for key := range tmpMap {
emails = append(emails, key)
}
sort.Strings(emails)
account = append(account, emails...)
res = append(res, account)
}
return res
}