-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDNAFinder.java
79 lines (68 loc) · 2.38 KB
/
DNAFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package br.com.app.endpoint;
public final class DNAFinder {
public static boolean isMutant(String[] dna) {
final int mutantTreshhold = 4;
boolean mutant = false;
for (int i = 0; (i < dna.length - mutantTreshhold && !mutant); i++) {
for (int j = 0; (j < dna[i].length() - mutantTreshhold && !mutant); j++) {
mutant |= findMutantSequence(dna, i, j, mutantTreshhold, 1, 1);
}
}
for (int i = 0; (i < dna.length - mutantTreshhold && !mutant); i++) {
for (int j = 0; (j < dna[i].length() && !mutant); j++) {
mutant |= findMutantSequence(dna, i, j, mutantTreshhold, 1, 0);
}
}
for (int i = 0; (i < dna.length && !mutant); i++) {
for (int j = 0; (j < dna[i].length() - mutantTreshhold && !mutant); j++) {
mutant |= findMutantSequence(dna, i, j, mutantTreshhold, 0, 1);
}
}
return mutant;
}
private static boolean findMutantSequence(String[] dna, int i, int j, Integer mutantTreshhold, int lineIncrement,
int columnIncrement) {
char current = dna[i].charAt(j);
int count = 1;
boolean mutant = true;
for (int k = i + lineIncrement, l = j + columnIncrement; k < dna.length && l < dna[k].length()
&& mutant; k += lineIncrement, l += columnIncrement) {
if (current != dna[k].charAt(l)) {
mutant = false;
} else {
count++;
}
}
return count >= mutantTreshhold;
}
public static boolean findMutantDNANeigborhood(String[] dna) {
final int mutantTreshhold = 4;
boolean res = false;
char[][] dnaMatrix = new char[dna.length + 1][dna.length + 1];
int[][][] dnaCountMatrix = new int[dna.length + 1][dna.length + 1][3];
for (int i = 0; i < dna.length; i++) {
for (int j = 0; j < dna.length; j++) {
dnaMatrix[i + 1][j + 1] = dna[i].charAt(j);
}
}
for (int i = 1; i < dnaMatrix.length; i++) {
for (int j = 1; j < dnaMatrix[0].length; j++) {
if (dnaMatrix[i][j] == dnaMatrix[i - 1][j - 1]) {
dnaCountMatrix[i][j][0] = dnaCountMatrix[i - 1][j - 1][0] + 1;
}
if (dnaMatrix[i][j] == dnaMatrix[i][j - 1]) {
dnaCountMatrix[i][j][1] = dnaCountMatrix[i][j - 1][1] + 1;
}
if (dnaMatrix[i][j] == dnaMatrix[i - 1][j]) {
dnaCountMatrix[i][j][2] = dnaCountMatrix[i - 1][j][2] + 1;
}
if (dnaCountMatrix[i][j][0] == mutantTreshhold - 1 || dnaCountMatrix[i][j][1] == mutantTreshhold - 1
|| dnaCountMatrix[i][j][2] == mutantTreshhold - 1) {
res = true;
break;
}
}
}
return res;
}
}