Simple C solution


  • 1
    S
    bool canConstruct(char* ransomNote, char* magazine) {
        unsigned int rason[256]={0};
        unsigned int mag[256]={0};
        int i=0;
        for(i=0;i<strlen(ransomNote);i++)
        {
            rason[ransomNote[i]]++;
        }
    
        for(i=0;i<strlen(magazine);i++)
        {
            mag[magazine[i]]++;
        }
    
        for(i=97;i<123;i++)
        {
             if(rason[i]>mag[i])
             {
              return false;
             }
        }
        return true;
    
    }
    

  • 0
    S

    Can you provide me the full code


  • 0
    W

    @senhisenhai
    i got a simpler solution

    bool canConstruct(char* ransomNote, char* magazine) {
        while(*ransomNote++!='\0'){
        	char *tmp=strchr(magazine,*(ransomNote-1));
        	if(!tmp) return false;
        	else *tmp=' ';}
        return true;
    }
    

Log in to reply
 

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