8ms C solution


  • -1
    P
        int freq[26]={0};
        if (*magazine == '\0' && *ransomNote != '\0')
            return false;
        while(*ransomNote != '\0')
        {
            freq[*ransomNote-'a']--;
            if(freq[*ransomNote-'a']<0)
            {
                freq[*ransomNote-'a']++;
                while(*magazine != '\0' && *ransomNote!=*magazine)
                    {
                        freq[*magazine-'a']++;
                        magazine++;
                    }
                if(*magazine == '\0')    
                    return false;
                else
                {
                    ransomNote++;    
                    magazine++;
                }
            }
            else
                ransomNote++;
        }
        return true;
    }

Log in to reply
 

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