Using map


  • 0
    2
    class Solution {
    public:
        string getHint(string secret, string guess) {
            unordered_map<int,int> map;
            //string s1=secret;
           // string s2=guess;
           string s3;
            int counttotal=0;
            int count=0;
            for(int i=0;i<secret.size();i++){
                map[secret[i]]++;
            }
            for(int i=0;i<guess.size();i++){
                if(--map[guess[i]]>=0){
                counttotal++;
                }
            }
            for(int i=0;i<secret.size();i++){
                if(secret[i]==guess[i]){
                    count++;
                }
            }
         s3=to_string(count)+"A"+to_string(counttotal-count)+"B";
         return s3;
        }
    };
    

    my first own code.


Log in to reply
 

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