Group Anagrams

Problem

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Example 2:

Input: strs = [""]
Output: [[""]]

Example 3:

Input: strs = ["a"]
Output: [["a"]]

Constraints:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.

Solution

First try!

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        var output = new ArrayList<List<String>>();
        int[][] a = new int[strs.length][26];
        Map<String, List<String>> m = new HashMap<>();

        for (int i = 0; i < strs.length; i++) {
            var str = strs[i];
            for (var c : str.toCharArray()) {
                a[i][(int) c - 'a'] += 1;
            }
            var key = Arrays.toString(a[i]);
            if (m.containsKey(key)) {
                m.get(key).add(str);
            } else {
                var l = new ArrayList<String>();
                l.add(str);
                m.put(key, l);
                output.add(l);
            }
        }

        return output;
    }
}

Cleaned up

Here’s the above solution, just cleaned up a bit

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        var a = new int[26];
        var m = new HashMap<String, List<String>>();

        for (var str : strs) {
            Arrays.fill(a, 0);
            for (var c : str.toCharArray()) {
                a[(int) c - 'a'] += 1;
            }
            var key = Arrays.toString(a);
            m.compute(key, (k, v) -> {
                if (v == null) {
                    v = new ArrayList<String>();
                }
                v.add(str);
                return v;
            });
        }

        return new ArrayList<>(m.values());
    }
}

Recent posts from blogs that I like

Sequoia introduces pinning to iCloud Drive

If you have Optimise Mac Storage enabled for iCloud Drive, this new feature lets you pin the files you want to be stored locally and not evicted. Full details.

via The Eclectic Light Company

Notes on running Go in the browser with WebAssembly

Recently I've had to compile Go to WebAssembly to run in the browser in a couple of small projects (#1, #2), and in general spent some time looking at WebAssembly. I find WebAssembly to be an exciting technology, both for the web and for other uses (e.g. with WASI); specifically, it's pretty great t...

via Eli Bendersky

I fixed the strawberry problem because OpenAI couldn't

Remember kids: real winners cheat

via Xe Iaso