MCPcopy Create free account
hub / github.com/careercup/ctci / estimateBad

Method estimateBad

java/Chapter 17/Question17_5/Question.java:88–117  ·  view source on GitHub ↗
(String g, String s)

Source from the content-addressed store, hash-verified

86 }
87
88 public static Result estimateBad(String g, String s) {
89 char[] guess = g.toCharArray();
90 char[] solution = s.toCharArray();
91 int hits = 0;
92 for (int i = 0; i < guess.length; i++) {
93 if (guess[i] == solution[i]) {
94 hits++;
95 solution[i] = '0';
96 guess[i] = '0';
97 }
98 }
99
100 int pseudohits = 0;
101
102 for (int i = 0; i < guess.length; i++) {
103 if (guess[i] != '0') {
104 for (int j = 0; j < solution.length; j++) {
105 if (solution[j] != '0') {
106 if (solution[j] == guess[i]) {
107 pseudohits++;
108 solution[j] = '0';
109 break;
110 }
111 }
112 }
113 }
114 }
115
116 return new Result(hits, pseudohits);
117 }
118
119 public static String randomString() {
120 int length = 4;

Callers 1

testMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected