Java char[] Solution


  • 2
    R
    public boolean isAnagram(String s, String t) 
    {
        // we do the following assumptions:
        // - s and t do not contain any whitespaces which have to be removed
        // - s and t do not contain ascii characters
        char[] sMap = map(s);
        char[] tMap = map(t);
        return (Arrays.equals(sMap, tMap));
    }
    
    private char[] map(String s)
    {
        char[] map = new char[128];
        for (int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            map[c] += 1;
        }
        return map;
    }

  • 0
    R

    shouldnt it be int[] map = new char[128]; instead of char[] map = new char[128];?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.