299. Bulls and Cows

Easy

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.

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.

Please note that both secret number and friend's guess may contain duplicate digits.

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow.

Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal.

package leetcode.easy;

public class BullsAndCows {
public String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
int[] secretCounts = new int[10];
int[] guessCounts = new int[10];
for (int i = 0; i < guess.length(); i++) {
char s = secret.charAt(i);
char g = guess.charAt(i);
if (s == g) {
bulls++;
} else {
secretCounts[Character.getNumericValue(s)]++;
guessCounts[Character.getNumericValue(g)]++;
}
}
for (int i = 0; i < 10; i++) {
cows = cows + Math.min(secretCounts[i], guessCounts[i]);
}
String res = bulls + "A" + cows + "B";
return res;
} @org.junit.Test
public void test() {
String secret1 = "1807";
String guess1 = "7810";
String secret2 = "1123";
String guess2 = "0111";
System.out.println(getHint(secret1, guess1));
System.out.println(getHint(secret2, guess2));
}
}

LeetCode_299. Bulls and Cows的更多相关文章

  1. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  2. LeetCode 299 Bulls and Cows

    Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  3. [Leetcode] Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  4. Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  5. 299. Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  6. Java [Leetcode 229]Bulls and Cows

    题目描述: You are playing the following Bulls and Cows game with your friend: You write down a number an ...

  7. [LeetCode299]Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  8. Bulls and Cows leetcode

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  9. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

随机推荐

  1. Java - Annotation使用

    本文转载于(这个写的很好):https://www.cnblogs.com/be-forward-to-help-others/p/6846821.html Annotation Annotation ...

  2. (尚014)Vue过渡与动画

    操作元素时有个过渡或动画的效果(渐变和移动的效果和放大缩小的效果) 过渡:trasition 动画:animation 1.vue动画的理解 1)操作css的trasition或animation(它 ...

  3. Zatree - Zabbix图表展示

    Zatree Zatree 是 一个php web的插件,做个展示树:可以提供host group的树形展示和在item里指定关键字查询及数据排序. 下载地址 可以根据zabbix不同版本下载:htt ...

  4. PHP-FPM 的工作整理

    1.php-fpm的配置文件 根据命令找到路径修改配置文件 ps -ef|grep php-fpm vim /home/php/etc/php-fpm.conf ;;;;;;;;;;;;;;;;;; ...

  5. rpm命令是RPM软件包的管理工具

    rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM ...

  6. 利用fgetc合并2个源文件的内容,到一个新的文件中

    #include <stdio.h> #include <stdlib.h> //功能: 合并2个源文件的内容,到一个新的文件中 int main(int a,char *ar ...

  7. Linux安装php yaml扩展

    1.首先得安装libyamlgit clone https://github.com/yaml/libyaml./bootstrap ./configure make make install 2.安 ...

  8. golang 无缓冲channel

    golang 无缓冲channel package main import "fmt" func main() { // 1S =1000ms //1ms = 1000us //1 ...

  9. Pytest权威教程14-缓存:使用跨执行状态

    目录 缓存:使用跨执行状态 使用方法 首先只重新运行故障或故障 上次运行中没有测试失败时的行为 新的config.cache对象 检查缓存内容 清除缓存内容 逐步修复失败用例 unittest.Tes ...

  10. Java与设计模式之单例模式(下) 安全的单例模式

          关于单例设计模式,<Java与设计模式之单例模式(上)六种实现方式>介绍了6种不同的单例模式,线程安全,本文介绍该如何保证单例模式最核心的作用——“实现该模式的类有且只有一个实 ...