Skip to content

Bug Report for accounts-merge #5873

@darkaddion

Description

@darkaddion

Bug Report for https://neetcode.io/problems/accounts-merge

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Running on Firefox, using the following code:

class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        List<Set<String>> mergedAccounts = new ArrayList<Set<String>>();
        for (List<String> account : accounts) {
            int firstMerge = -1;
            for (int accountIndex = 0; accountIndex < mergedAccounts.size(); accountIndex++) {
                Set<String> possibleMerge = mergedAccounts.get(accountIndex);
                if (possibleMerge.contains(account.get(0))) {
                    // Same name, check for matching emails
                    for (int emailIndex = 1; emailIndex < account.size(); emailIndex++) {
                        if (possibleMerge.contains(account.get(emailIndex))) {
                            // Matching email, merge accounts
                            if (firstMerge != -1) {
                                // Previous account is also a match, so all emails should go there
                                Set<String> mergeInto = mergedAccounts.get(firstMerge);
                                for (String email : possibleMerge) {
                                    mergeInto.add(email);
                                }
                                mergedAccounts.remove(accountIndex);
                                accountIndex--;
                            } else {
                                firstMerge = accountIndex;
                                for (String email : account) {
                                    possibleMerge.add(email);
                                }
                            }
                        }
                    }
                }
            }
            if (firstMerge == -1) {
                // No matches, add account
                Set<String> accountSet = new HashSet<String>();
                for (String email : account) {
                    accountSet.add(email);
                }
                mergedAccounts.add(accountSet);
            }
        }
        List<List<String>> result = new ArrayList<List<String>>();
        for (List<String> account : accounts) {
            String name = account.get(0);
            for (int entryIndex = 0; entryIndex < mergedAccounts.size(); entryIndex++) {
                Set<String> entry = mergedAccounts.get(entryIndex);
                if (entry.contains(name)) {
                    List<String> resultEntry = new ArrayList<String>(entry);
                    resultEntry.remove(name);
                    resultEntry.add(0, name);
                    result.add(resultEntry);
                    mergedAccounts.remove(entry);
                    if (mergedAccounts.size() == 0) {
                        break;
                    }
                    entryIndex--;
                }
            }
        }
        return result;
        /*Map<String, List<Set<String>>> nameToEmails = new HashMap<String, List<Set<String>>>();
        for (List<String> account : accounts) {
            String name = account.get(0);
            Set<String> emails = new HashSet<String>();
            for (int index = 1; index < account.size(); index++) {
                emails.add(account.get(index));
            }
            List<Set<String>> possibleMatches = nameToEmails.getOrDefault(name, new ArrayList<Set<String>>());
            for (int matchIndex = 0; matchIndex < possibleMatches.size(); matchIndex++) {
                Set<String> possibleMatch = possibleMatches.get(matchIndex);
                for (int emailIndex = 1; emailIndex < account.size(); emailIndex++) {
                    if (possibleMatch.contains(account.get(emailIndex))) {
                        // Merge accounts
                        possibleMatches.remove(possibleMatch);
                        for (String email : possibleMatch) {
                            emails.add(email);
                        }
                        break;
                    }
                }
            }
            possibleMatches.add(emails);
            nameToEmails.put(name, possibleMatches);
        }
        List<List<String>> result = new ArrayList<List<String>>();
        for (Map.Entry<String, List<Set<String>>> entry : nameToEmails.entrySet()) {
            for (Set<String> emails : entry.getValue()) {
                List<String> resultEntry = new ArrayList<String>(emails);
                resultEntry.add(0, entry.getKey());
                result.add(resultEntry);
            }
        }
        return result;*/
    }
}

With the following test cast:

accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]

This test case is case 2/16. When submitting the code, the test fails with an java.lang.IndexOutOfBoundsException at line 19, mergedAccounts.remove(accountIndex);.

Running locally, this test fails due to incorrect output (Actual [["John","johnnybravo@mail.com"],["Mary","mary@mail.com"]] vs expected [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]] ), but does not throw any errors.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions