About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Lexicographical Order Problem

Given a bunch of key-value pairs, for each unique key find 1) the number of values and 2) the lexicographically greatest value.

Example One

Input is an array of strings, each with a key and a value separated by a space:

["key1 abcd",

"key2 zzz",

"key1 hello",

"key3 world",

"key1 hello"]

Output is an array of strings with unique keys followed by a colon, the total number of values, a comma, and the lexicographically greatest of the values associated with that key in the input:

["key1:3,hello",

"key2:1,zzz",

"key3:1,world"]

The order or strings in the output is insignificant; these same strings in a different order are also a correct output.

Example Two

Input:

["mark zuckerberg",

"tim cook",

"mark twain"]

Output:

["mark:2,zuckerberg",

"tim:1,cook"]

or

["tim:1,cook",

"mark:2,zuckerberg"]

Notes

Order of strings in the output does not matter.

Constraints:

● Keys and values consist of lowercase letters and digits.

● 1

● 1

● 1

● Keys can repeat.

● Values can repeat.

Solution

We provided one solution. It uses a hashmap to store the current number of values and the lexicographically greatest value for each key found so far as we iterate over the input. To process another string from the input we identify the key and value and then look for a value already present in the hashmap for this key. If one is found, we increment the number of values for this key and make sure the hashmap has the lexicographically greater value of the one it already has and one from the current input string. If no value for the current key is found in the hashmap, we simply put the current one there.

After processing all the input strings in this way, we just need to convert the hashmap data to the output format.

Time Complexity:

O(n*(longest key + longest value)) because comparing two strings of length k takes O(k) time.

Auxiliary Space Used:

O(n*(longest key + longest value)) because of the hashmap.

Space Complexity:

O(n*(longest key + longest value)).


// -------- START --------
    static String[] solve(String[] arr)
    {
        Map map = new HashMap<>();

        for (String input : arr)
        {
            String[] pair = input.split(" ");
            String key = pair[0];
            String val = pair[1];

            Entry entry = map.get(key);

            if (entry == null)
            {
                map.put(key, new Entry(val)); // The new entry has count=1.
            }
            else
            {
                entry.count++;
                if (val.compareTo(entry.lexGreatest) > 0)
                {
                    entry.lexGreatest = val;
                }
            }
        }

        String[] results = new String[map.size()];
        int i = 0;
        for (Map.Entry e: map.entrySet())
        {
            results[i++] = e.getKey() + ":" + e.getValue().count + "," + e.getValue().lexGreatest;
        }

        return results;
    }

    static class Entry
    {
        int count = 1;
        String lexGreatest;

        Entry(String lexGreatest)
        {
            this.lexGreatest = lexGreatest;
        }
    }
    // -------- END --------