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 match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number.
For example:
Secret number: "1807"
Friend's guess: "7810"
Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return "1A3B".
Please note that both secret number and friend's guess may contain duplicate digits, for example:
Secret number: "1123"
Friend's guess: "0111"
In this case, the 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".
You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.
public class Solution {
public String getHint(String secret, String guess) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int As = ;
int Bs = ;
for (int i = ; i < secret.length(); i++) {
map.put(secret.charAt(i), map.getOrDefault(secret.charAt(i), ) + );
}
// check As
for (int i = ; i < secret.length(); i++) {
char gLetter = guess.charAt(i);
if (secret.charAt(i) == gLetter) {
As++;
map.put(gLetter, map.get(gLetter) - );
}
}
// check Bs
for (int i = ; i < secret.length(); i++) {
char gLetter = guess.charAt(i);
if (secret.charAt(i) != gLetter) {
if (map.getOrDefault(gLetter, ) != ) {
Bs++;
map.put(gLetter, map.get(gLetter) - );
}
}
}
return As + "A" + Bs + "B";
}
}
Bulls and Cows的更多相关文章
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- Java [Leetcode 229]Bulls and Cows
题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...
- [LeetCode299]Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- Bulls and Cows leetcode
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- LeetCode(45)-Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
随机推荐
- Xcode的一些有用的插件
** --Alcatraz:Xcode插件管理 ** 安装:curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/ ...
- POJ1860Currency Exchange(Bellman + 正权回路)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23938 Accepted: 867 ...
- linux磁盘空间清理
由于当初安装系统设计不合理,有些分区的过小,以及网络通讯故障等造成日志文件速度增长等其他原因都可以表现为磁盘空间满,造成无法读写磁盘,应用程序无法执行等.下面就给你支几招(以/home空间满为例): ...
- Json序列化对象
之前都是用的二进制的序列化方法,是.net自带的,但是最常用到的还是Json序列化 (1)只需要调用 Newtonsoft.Json.dll 即可 public class JsonTools { / ...
- tomcat Host及Context 配置
参考资料: 一.Host配置 对一个Tomcat,可以配置多台虚拟主机.简单地说,就是让一台服务器可以对应多个主机名.这在Tomcat中称之为Host.要求每个Host的Name必须唯一. 配置方法: ...
- hdu 1284 钱币兑换问题(动态规划)
Ac code : 完全背包: #include<stdio.h> #include<string.h> int dp[4][40000]; int main(void) { ...
- Linux下SVN安装配置和使用中遇到的问题
两个命令: svn info :显示版本库信息,svn的下载url等. svn co https://xxxxx/xxx wodemulu (通过我的目录制定co的文件夹) svn st:显示 ...
- mysql is marked as crashed and should be repaired错误
1.mysql数据存放路径默认为/var/lib/mysql/目录 2.用myisamchk命令修复数据表,如: myisamchk -c -r talbe.MYI
- .NET中使用Memcached的相关资源整理
Memcached官方站点:http://www.danga.com/memcached/ Memcached Win32 1.2.6下载:http://code.jellycan.com/memca ...
- Windows 下的.NET+ Memcached安装
转载请标明出处: http://www.yaosansi.com/ 原文:http://www.yaosansi.com/post/1396.html Memcached官方:http://danga ...