My single comprehensive solution


  • 0
    Z
    class Solution {
    public:
        string getHint(string secret, string guess) 
        {
            unordered_map<char,int> myMap;
            int bull=0,cow=0;
            
            for(auto i=secret.begin();i!=secret.end();i++)
            {
                if(myMap.find(*i)!=myMap.end())
                    myMap[*i]++;
                else
                    myMap[*i]=1;
            }
            
            for(int i=0;i<guess.length();i++)
            {
                if(secret[i]==guess[i])
                {
                    bull++;
                    myMap[secret[i]]--;
                }
            }
            
            for(int i=0;i<guess.length();i++)
            {
                if(secret[i]!=guess[i] && myMap[guess[i]]>0)
                {
                    cow++;
                    myMap[guess[i]]--;
                }
            }
            
            return to_string(bull)+'A'+to_string(cow)+'B';
        }
    };

Log in to reply
 

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