英文原文
You are playing the Bulls and Cows game with your friend.
You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:
- The number of "bulls", which are digits in the guess that are in the correct position.
- The number of "cows", which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
Given the secret number secret and your friend's guess guess, return the hint for your friend's guess.
The hint should be formatted as "xAyB", where x is the number of bulls and y is the number of cows. Note that both secret and guess may contain duplicate digits.
Example 1:
Input: secret = "1807", guess = "7810" Output: "1A3B" Explanation: Bulls are connected with a '|' and cows are underlined: "1807" | "7810"
Example 2:
Input: secret = "1123", guess = "0111" Output: "1A1B" Explanation: Bulls are connected with a '|' and cows are underlined: "1123" "1123" | or | "0111" "0111" Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
Example 3:
Input: secret = "1", guess = "0" Output: "0A0B"
Example 4:
Input: secret = "1", guess = "1" Output: "1A0B"
Constraints:
1 <= secret.length, guess.length <= 1000secret.length == guess.lengthsecretandguessconsist of digits only.
中文题目
你在和朋友一起玩 猜数字(Bulls and Cows)游戏,该游戏规则如下:
写出一个秘密数字,并请朋友猜这个数字是多少。朋友每猜测一次,你就会给他一个包含下述信息的提示:
- 猜测数字中有多少位属于数字和确切位置都猜对了(称为 "Bulls", 公牛),
- 有多少位属于数字猜对了但是位置不对(称为 "Cows", 奶牛)。也就是说,这次猜测中有多少位非公牛数字可以通过重新排列转换成公牛数字。
给你一个秘密数字 secret 和朋友猜测的数字 guess ,请你返回对朋友这次猜测的提示。
提示的格式为 "xAyB" ,x 是公牛个数, y 是奶牛个数,A 表示公牛,B 表示奶牛。
请注意秘密数字和朋友猜测的数字都可能含有重复数字。
示例 1:
输入: secret = "1807", guess = "7810" 输出: "1A3B" 解释: 数字和位置都对(公牛)用 '|' 连接,数字猜对位置不对(奶牛)的采用斜体加粗标识。 "1807" | "7810"
示例 2:
输入: secret = "1123", guess = "0111" 输出: "1A1B" 解释: 数字和位置都对(公牛)用 '|' 连接,数字猜对位置不对(奶牛)的采用斜体加粗标识。 "1123" "1123" | or | "0111" "0111" 注意,两个不匹配的 1 中,只有一个会算作奶牛(数字猜对位置不对)。通过重新排列非公牛数字,其中仅有一个 1 可以成为公牛数字。
示例 3:
输入:secret = "1", guess = "0" 输出:"0A0B"
示例 4:
输入:secret = "1", guess = "1" 输出:"1A0B"
提示:
1 <= secret.length, guess.length <= 1000secret.length == guess.lengthsecret和guess仅由数字组成
通过代码
高赞题解
模拟
根据题意,我们可以对 $secret$ 和 $guess$ 进行诸位比较,统计公牛数量 $a$ 和奶牛数量 $b$。
对于字符相同的位置,我们可以直接对 $a$ 进行自增;对于字符不同的位置,使用「哈希表」进行分别统计 $secret$ 和 $guess$ 的词频,某个数字 $x$ 在两者词频中的较小值,即为该数字对应的奶牛数量,统计所有数字 $[0, 9]$ 的奶牛数量总和即为 $b$。
代码:
[]class Solution { public String getHint(String secret, String guess) { int n = secret.length(); int a = 0, b = 0; int[] cnt1 = new int[10], cnt2 = new int[10]; for (int i = 0; i < n; i++) { int c1 = secret.charAt(i) - '0', c2= guess.charAt(i) - '0'; if (c1 == c2) { a++; } else { cnt1[c1]++; cnt2[c2]++; } } for (int i = 0; i < 10; i++) b += Math.min(cnt1[i], cnt2[i]); return a + "A" + b + "B"; } }
- 时间复杂度:$O(n)$
- 空间复杂度:令 $C$ 为字符集大小,$C$ 固定为 $10$。复杂度为 $O(C)$
其他「模拟」相关内容
题太简单?不如来学习热乎的 $Trie$ 四部曲的最终章 「可删除/可计数/持久化」Trie ,相关阅读:
或是考虑加练如下「模拟」题目 🍭🍭
注:以上目录整理来自 wiki,任何形式的转载引用请保留出处。
最后
如果有帮助到你,请给题解点个赞和收藏,让更多的人看到 ~ (“▔□▔)/
也欢迎你 关注我(公主号后台回复「送书」即可参与长期看题解学算法送实体书活动)或 加入「组队打卡」小群 ,提供写「证明」&「思路」的高质量题解。
所有题解已经加入 刷题指南,欢迎 star 哦 ~
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 63239 | 112076 | 56.4% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|