Solution without Hashing


  • 0
    K
    public static  String getHint(String secret, String guess) {
    
        char sec[] = secret.toCharArray();
        char gue[] = guess.toCharArray();
        int bull = 0;
        int cows = 0;
    
        //To count the bulls
        for(int i=0; i< sec.length;i++){
    
            if(sec[i] == gue[i]){
                bull++;
                secret = secret.replaceFirst(String.valueOf(gue[i]),"*");
                guess = guess.replaceFirst(String.valueOf(gue[i]),"*");
            }
        }
    
        char gue1[] = guess.toCharArray();
        //To count the cows
        for(int i=0; i< sec.length;i++){
            if (gue1[i] != '*'){
                if (secret.contains(String.valueOf(gue1[i]))){
                    cows++;
                    secret = secret.replaceFirst(String.valueOf(gue[i]),"*");
                }
            }
    
        }
    
        String s = bull+"A"+cows+"B";
    
        return s;
    
    }

Log in to reply
 

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