JavaScript solution


  • 1
    L
    function getHint(secret, guess) {
      var map = {};
      var A = 0;
      var B = 0;
      for (var i = 0; i < 10; i++) map[i] = 0;
      for (i = 0; i < secret.length; i++) {
        if (secret[i] === guess[i]) A++;
        else {
          map[secret[i]]++;
          B += map[secret[i]] <= 0 ? 1 : 0;
          map[guess[i]]--;
          B += map[guess[i]] >= 0 ? 1 : 0;
        }
      }
      return A + 'A' + B + 'B';
    }

Log in to reply
 

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