5ms easy-understand java


  • 0

    Try to use array to store '0' - '9' digit.

    public class Solution {
    	
    public String getHint(String secret, String guess) {
        int [] se = new int[10];
        int bulls=0, cows = 0;
        for (int i = 0;i<secret.length();++i)
        {
        	if (secret.charAt(i) == guess.charAt(i)) bulls++;
        	else se[secret.charAt(i)-'0']++;
        }
        for (int i = 0;i<guess.length();++i)
        {
        	if (se[guess.charAt(i)-'0'] > 0 && secret.charAt(i) != guess.charAt(i))
        	{
        		se[guess.charAt(i)-'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.