88ms slow AC solutions, where to improve ?


  • 0
    C

    Guess my logic is clear, but can someone tell me which of my sentences takes too much time? If I am going to optimize the code, where to start? Thank you guys.

    class Solution {
    public:
        bool canConstruct(string ransomNote, string magazine) {
            if(ransomNote.size()>magazine.size())   return false;
            
            unordered_map<char,int>     ransom;
            unordered_map<char,int>     maga;
            
            for(int i = 0;i<ransomNote.size();i++)
                ransom[ransomNote[i]]++;
                
            for(int i = 0;i<magazine.size();i++)
                maga[magazine[i]]++;
            
            if(ransom.size()>maga.size())   return false;
            
            for(auto iterR = ransom.begin();iterR != ransom.end();iterR++)
            {
                if(iterR->second > maga[iterR->first])  return false;
            }
            
            return true;
        }
    };
    

Log in to reply
 

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