Java solution without HashTable


  • 5
    L

    public String getHint(String secret, String guess) {

        int bull=0;
        int cow=0;
        int[] marks=new int[10];
        int[] markg=new int[10];
        for(int i =0;i<secret.length();i++){
            if(secret.charAt(i)==guess.charAt(i))
            bull++;
            marks[secret.charAt(i)-'0']++;
            markg[guess.charAt(i)-'0']++;
        }
        for(int i =0;i<10;i++){
            cow+=Math.min(marks[i],markg[i]);
        }
        StringBuffer sb=new StringBuffer();
        sb.append(bull);
        sb.append("A");
        sb.append(cow-bull);
        sb.append("B");
        String returnStr=new String(sb);
        return returnStr;
    
    }

Log in to reply
 

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