Bit Vector STORE alphabet into corresponding pos
Return more than two variables = > create a class
R Y G B
Case1: R G G B
Gues1: Y R G B
p-hit hit hit
=> ONLT told : 1 p-hit and 2 hits
check color and position =>
current hit = 0;
if color match and position match => current hit =2 ( hit)
O(n^2) => O(n) bit vector, int has 32 bit enough for 26 alphabets,
2. Implementation
// Assume all Capital
public static class Result { public int hits; public int pseudoHits; } public static Result estimate(String guess, String solution) { Result res = new Result(); // validate the input if (guess == null || solution == null) return res; int solution_mask = 0; for (int i = 0; i < 4; i++ ) { int val = solution.charAt(i) - 'A'; // 'A'->0, 'B'->1, 'C'->2 // NOTE :Set bits solution_mask |= 1<<(1+val);// 'A'->1, 'B'->2, 'C'->3, 26 aplhabets } for (int i = 0 ; i < 4 ; i ++) { if (guess.charAt(i) == solution.charAt(i)) ++res.hits; // NOTE: Test bits else if ( (solution_mask & ( 1<< (1+guess.charAt(i) - 'A') )) >=1 ) ++res.pseudoHits; } return res; }3. similar Ones
No comments:
Post a Comment