-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTwoCharacters.java
More file actions
59 lines (48 loc) ยท 1.48 KB
/
TwoCharacters.java
File metadata and controls
59 lines (48 loc) ยท 1.48 KB
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
package hackerrank;
public class TwoCharacters {
public static final int NUM_LETTERS = 26;
static int alternate(int length, String s) {
int maxPattern = 0;
if(s.length() == 1)//Edge case where length is 1
{
return maxPattern;
}
/* Create arrays representing the 26^2 subproblems */
int[][] pair = new int[NUM_LETTERS][NUM_LETTERS];
int[][] count = new int[NUM_LETTERS][NUM_LETTERS];
for (int i = 0; i < s.length(); i++) {
char letter = s.charAt(i);
int letterNum = letter - 'a';
/* Update row */
for (int col = 0; col < NUM_LETTERS; col++) {
if (pair[letterNum][col] == letter) {
count[letterNum][col] = -1;
}
if (count[letterNum][col] != -1) {
pair[letterNum][col] = letter;
count[letterNum][col]++;
}
}
/* Update column */
for (int row = 0; row < NUM_LETTERS; row++) {
if (pair[row][letterNum] == letter) {
count[row][letterNum] = -1;
}
if (count[row][letterNum] != -1) {
pair[row][letterNum] = letter;
count[row][letterNum]++;
}
}
}
/* Find max in "count" array */
for (int row = 0; row < NUM_LETTERS; row++) {
for (int col = 0; col < NUM_LETTERS; col++) {
maxPattern = Math.max(maxPattern, count[row][col]);
}
}
return maxPattern;
}
public static void main(String[] args) {
System.out.println(alternate(10, "beabeefeab")+", ans: 5");
}
}