Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created September 22, 2013 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wszdwp/6663203 to your computer and use it in GitHub Desktop.
Save wszdwp/6663203 to your computer and use it in GitHub Desktop.
Cracking the interview Question19.5
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