Java not good solution, help to understand


  • 0
    X
    public class Solution {
        public String getHint(String secret, String guess) {
            int a=0,b=0;
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            for(int i=0;i<secret.length();i++){
                map.put(secret.charAt(i), map.getOrDefault(secret.charAt(i), 0) + 1);
            }
            for(int i=0;i<secret.length();i++){
                if(secret.charAt(i)==guess.charAt(i)){
                    a++;
                    map.put(secret.charAt(i), map.get(secret.charAt(i)) - 1);
                }
            }
            for(int i = 0; i < secret.length(); i++){
                if(secret.charAt(i)!=guess.charAt(i)){           
                if(map.containsKey(guess.charAt(i)) && map.get(guess.charAt(i)) > 0){
                    b++;
                    map.put(guess.charAt(i), map.get(guess.charAt(i)) - 1); 
                }
            }}
            return a+"A"+b+"B";
        }
    }

Log in to reply
 

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