C++ 4ms one pass


  • 1
    L
    class Solution {
    public:
        string getHint(string secret, string guess) {
            int s[10] = {}, g[10] = {};
            int n = secret.size();
            int A = 0, B = 0;
            for (int i = 0; i < n; i++) {
                if (secret[i] == guess[i]){
                    A++;
                    continue;
                }
                int ss = secret[i] - '0', gg = guess[i] - '0';
                s[ss]++;
                g[gg]++;
                B += (g[ss] && s[ss]) + (g[gg] && s[gg]);
                if (g[ss] && s[ss]) {
                    g[ss]--;
                    s[ss]--;
                }
                if (g[gg] && s[gg]) {
                    g[gg]--;
                    s[gg]--;
                }
            }
            return to_string(A) + 'A' + to_string(B) + 'B';
        }
    };

  • 0
    L

    Thumb up! So clever!


  • 0
    U

    you are very very clever.


Log in to reply
 

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