Java 11ms beats 99.94%


  • 0
    Z

    refine the array solution a little bit to make it faster

    public boolean canConstruct(String ransomNote, String magazine) {
        int[] count = new int['a' + 26];
        char[] m = magazine.toCharArray();
        char[] r = ransomNote.toCharArray();
        for(char c : m) count[c] ++;
        for(char c : r){
            if(--count[c] < 0) return false;
        }
        return true;
    }
    

Log in to reply
 

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