Java one pass solution O(n) time complexity


  • 1
    D
        public String getHint(String secret, String guess) {
            char[] seChar = secret.toCharArray();
            char[] guChar = guess.toCharArray();
            int[] countOfNum = new int[10];
            int cows = 0, bulls = 0;
            for(int i = 0; i < secret.length(); i++){
                if(secret.charAt(i) == guess.charAt(i)){
                    bulls++;
                }
                else{
                	if(countOfNum[seChar[i]-'0']++<0){
                		cows++;
                	}
                    if(countOfNum[guChar[i]-'0']-->0){
                    	cows++;
                    }
                }
            }
            return bulls + "A" + cows + "B";
        }
    

Log in to reply
 

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