My 3ms Java solution may help u


  • 34
    public class Solution {
        public String getHint(String secret, String guess) {
            int len = secret.length();
    		int[] secretarr = new int[10];
    		int[] guessarr = new int[10];
    		int bull = 0, cow = 0;
    		for (int i = 0; i < len; ++i) {
    			if (secret.charAt(i) == guess.charAt(i)) {
    				++bull;
    			} else {
    				++secretarr[secret.charAt(i) - '0'];
    				++guessarr[guess.charAt(i) - '0'];
    			}
    		}
    		for (int i = 0; i < 10; ++i) {
    			cow += Math.min(secretarr[i], guessarr[i]);
    		}
    		return "" + bull + "A" + cow + "B";
        }
    }

  • 0
    J

    Smart solution! Thank you!


  • 0
    A

    Nice solution!!!


  • 0
    R

    Great solution. Easy and clear logic. Thanks


  • 0
    S

    Good solution, and you can use one array instead


Log in to reply
 

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