Java solution beats 99%, no hashmap, only one loop


  • 5
    A
    public class Solution {
        public String getHint(String secret, String guess) {
            int s, g, size = secret.length();
            int bulls = 0, cows = 0;
            int [] nums = new int [10];
            for (int i = 0; i < size; i++) {
                s = secret.charAt(i) - '0';
                g = guess.charAt(i) - '0';
                if (s == g)
                    bulls ++;
                else{
                    if (nums[s] < 0)
                        cows++;
                    nums[s]++;
                    if (nums[g] > 0)
                        cows++;
                    nums[g]--;
                }
            }
            return bulls + "A" + cows + "B";
        }
    }
    

  • 0
    Z

    Really interesting! Thank you!


Log in to reply
 

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