Jan 13 - Bulls and Cows; String; HashSet;

直接用hashmap做的:

代码:

public class Solution {
    public String getHint(String secret, String guess) {
        if(secret == null || guess == null) return null;
        Map<Character, Integer> secretMap = new HashMap<>();
        Map<Character, Integer> guessMap = new HashMap<>();
        List<Character> list = new ArrayList<>();
        int len = secret.length();
        int numA = 0;
        int numB = 0;
        for(int i = 0; i < len; i++){
            char c1 = secret.charAt(i);
            char c2 = guess.charAt(i);
            if(c1 == c2) numA++;
            else{
                if(secretMap.containsKey(c1)) secretMap.put(c1, secretMap.get(c1)+1);
                else{
                    secretMap.put(c1, 1);
                    list.add(c1);
                }
                if(guessMap.containsKey(c2)) guessMap.put(c2, guessMap.get(c2)+1);
                else guessMap.put(c2, 1);
            }
        }
        for(int i = 0; i < list.size(); i++){
            char c = list.get(i);
            if(secretMap.containsKey(c) && guessMap.containsKey(c)) numB += Math.min(secretMap.get(c), guessMap.get(c));
        }
        return numA+"A"+numB+"B";
    }
}

  

时间: 2024-11-05 15:56:33

Jan 13 - Bulls and Cows; String; HashSet;的相关文章

[LeetCode] Bulls and Cows

Bulls and Cows You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits

299. Bulls and Cows

题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess matc

LeetCode:Bulls and Cows - 猜数字游戏

1.题目名称 Bulls and Cows(猜数字游戏) 2.题目地址 https://leetcode.com/problems/bulls-and-cows/ 3.题目内容 英文:You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend g

Java [Leetcode 229]Bulls and Cows

题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess ma

[LeetCode] Bulls and Cows 公母牛游戏

You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend guesses a number, you give a hint, the hint tells your friend how many digits are in the corr

[LeetCode299]Bulls and Cows

题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess matc

Leetcode题目:Bulls and Cows

题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess matc

【Leetcode】Bulls and Cows

题目链接:https://leetcode.com/problems/bulls-and-cows/ 题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hin

Bulls and Cows leetcode

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match yo