721. Accounts Merge #
Problem #
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", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation:
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
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', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'],
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Note:
- The length of
accountswill be in the range[1, 1000]. - The length of
accounts[i]will be in the range[1, 10]. - The length of
accounts[i][j]will be in the range[1, 30].
Problem Summary #
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:
- The length of accounts will be in the range [1, 1000].
- The length of accounts[i] will be in the range [1, 10].
- The length of accounts[i][j] will be in the range [1, 30].
Solution Ideas #
- Given a bunch of accounts and their corresponding emails. The requirement is to merge multiple email accounts of the same person. How do we determine whether they are the same person? If the person’s name and one of the emails belonging to them are the same, then it is determined that these are emails of the same person, and these emails are merged.
- The idea for this problem is Union-Find. However, if a brute-force merging method is used, the time complexity is very poor. The optimization method is to number each group of data first: assign numbers to people, and assign numbers to each email. This mapping relationship is recorded with a
map. Then use theunion()operation of Union-Find to merge these numbers. Finally, concatenate the person’s number with the corresponding email numbers. - There are 2 relatively “tricky” points in this problem: the email list of users that do not need to be merged also needs to be sorted and deduplicated, and all email sets of the same person must be merged together. See the test cases for details. However, the problem statement also mentions these points, so it cannot really be considered a trick in the problem; it can only be blamed on not paying attention to these boundary cases.
Code #
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
}