6ms C accepted solution


  • 0
    L

    At some point in time, line

    if(mag_len < ran_len) return false;
    

    separated me from "Time Limit Exceeded".

    bool canConstruct(char* ransomNote, char* magazine) {
        int count[128] = {0}, i, ran_len = strlen(ransomNote), mag_len = strlen(magazine);
        
        if(mag_len < ran_len) return false;
        for(i = 0; i < mag_len; i++) count[magazine[i]]++;
        for(i = 0; i < ran_len; i++) count[ransomNote[i]]--;
        for(i = 0; i < 128; i++) if(count[i] < 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.