My C++ solution using a single array in 0(n) time


  • 0
    M

    class Solution {
    public:
    string getHint(string s1, string s2) {

        int count[10] = {0};
        
        int b=0;
        int c=0;
        
        for(int i=0;i<s1.length();i++)
        {
            if(s1[i] == s2[i])
                b++;
            else
            {
                count[s1[i]-'0']++;
                if(count[s1[i]-'0'] <= 0)
                    c++;
                count[s2[i]-'0']--;
                if(count[s2[i]-'0'] >= 0)
                    c++;    
            }
        }
        
        string res = to_string(b) + "A" + to_string(c) + "B";
        return res;
    }
    

    };


Log in to reply
 

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