C 8ms Simple Array Solution


  • 0
    F

    Runtime: 8ms
    Runtime Bound: O(m+n)
    Space: O(1)

        int i = 0;
        int *keys = malloc(26 * sizeof(int));
        memset(keys, 0, 26 * sizeof(int));
        char *currNote = NULL;
        while((currNote = magazine[i ++])) {
            keys[(int)currNote - 'a'] ++;
        }
        i = 0;
        while((currNote = ransomNote[i ++])) {
            int idx = (int)currNote - 'a';
            if(keys[idx] > 0) {
                keys[idx] --;
            } else {
                return 0;
            }
        }
        return 1;
    }

Log in to reply
 

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