Unicode and supplementary characters
The follow-up question is really tricky. For example, in Java, mentioning Unicode usually makes one thing of the char type and a HashMap. However, strictly spea
The follow-up question is really tricky. For example, in Java, mentioning Unicode usually makes one thing of the `char` type and a `HashMap`. However, strictly speaking, that will only allow to handle BMP, not the entire Unicode. And good code should really be able to handle everything (as `StringBuilder.reverse` does, for example).\n\nHowever, it turns out that LeetCode doesn't even support anything but US-ASCII. I've just tried to run several test cases and it breaks saying \n\n> 'ascii' codec can't encode character u'\\U0002f81a' in position 1:\n> ordinal not in range(128)"\n\nSetting that aside, this problem isn't even strictly defined for Unicode characters, at least in Java. Supplementary characters are represented by surrogate pairs, but what if one of the strings contains a surrogate that is not a part of a valid pair? I think it makes sense to define an anagram as a string that has the same count of each BMP character, valid surrogate pair and lone surrogate.\n\nFor example, `"\\uD87E\\uDC1A\\uD87E\\uDC28"` is a valid anagram of `"\\uD87E\\uDC28\\uD87E\\uDC1A"`, but not of `"\\uDC1A\\uD87E\\uD87E\\uDC28"`.\n\n**Update:** looks like Discuss is broken for supplementary characters too. I've just tried to post an example, but it got truncated at the first ideograph. Good thing I always Ctrl+C the whole thing before clicking the post button.\n\nHere is the correct solution, as far as I was able to test it on my PC:\n\n    public boolean isAnagram(String s, String t) {\n        if (s.length() != t.length()) {\n            return false;\n        }\n        Map<Integer, Integer> sm = computeFrequencyMap(s);\n        Map<Integer, Integer> tm = computeFrequencyMap(t);\n        return sm.equals(tm);\n    }\n\n    private Map<Integer, Integer> computeFrequencyMap(String s) {\n        Map<Integer, Integer> sm = new HashMap<>();\n        for (int i = 0; i < s.length(); ++i) {\n            char cs = s.charAt(i);\n            int cp;\n            if (i < s.length() - 1 && Character.isHighSurrogate(cs)) {\n                char cn = s.charAt(i + 1);\n                cp = Character.isLowSurrogate(cn) ? Character.toCodePoint(cs, cn) : cs;\n            } else {\n                cp = cs;\n            }\n            i = cp == cs ? i : i + 1;\n            sm.put(cp, sm.getOrDefault(cp, 0) + 1);\n        }\n        return sm;\n    }
['Java']