Created
September 22, 2013 19:56
-
-
Save wszdwp/6663203 to your computer and use it in GitHub Desktop.
Cracking the interview Question19.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Question195 | |
{ | |
public static class Result | |
{ | |
private int hits; | |
private int pseudohits; | |
} | |
public static Result estimate(String guess, String solution) { | |
Result res = new Result(); | |
int solutionMask = 0; | |
if (guess.length() != solution.length()) return null; | |
for (int i = 0; i < solution.length(); i++) { | |
solutionMask |= 1 << ( 1 + solution.charAt(i) - 'A' ); | |
} | |
for (int i = 0; i < guess.length(); i++) { | |
if( solution.charAt(i) == guess.charAt(i) ) { | |
++res.hits; | |
} else if( (( 1 << (1 + guess.charAt(i) - 'A') ) & solutionMask ) >= 1) { | |
++res.pseudohits; | |
} | |
} | |
return res; | |
} | |
public static void main(String[] args) { | |
String guess = "YRGB"; | |
String solution = "RGGB"; | |
Result result1 = estimate( guess, solution); | |
System.out.println("hits Expected(2):" + result1.hits); | |
System.out.println("hits Expected(1):" +result1.pseudohits); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment