-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_2351.java
67 lines (61 loc) · 1.8 KB
/
prob_2351.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
import java.util.Arrays;
/**
* 2351. First Letter to Appear Twice
* <p>
* Easy
* <p>
* Given a string s consisting of lowercase English letters, return the first letter to appear twice.
* <p>
* Note:
* <p>
* A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.
* s will contain at least one letter that appears twice.
* <p>
* Constraints:
* <p>
* 2 <= s.length <= 100
* <p>
* s consists of lowercase English letters.
* <p>
* s has at least one repeated letter.
*/
public class prob_2351 {
public static void main(String[] args) {
Solution_2351 solution = new Solution_2351();
String[] s = {"abccbaacz"};
System.out.println(solution.repeatedCharacter(s[0]));
}
}
/**
* Solution using simple array traversal
* <p>
* Time Complexity - O(N), Space Complexity - O(N)
*/
class Solution_2351 {
public char repeatedCharacter(String s) {
int[] alphabet = new int[26];
boolean[] hasRepeated = new boolean[26];
char[] string = s.toCharArray();
for (int i = 0; i < string.length; i++) {
if (hasRepeated[string[i] - 'a']) continue;
if (alphabet[string[i] - 'a'] == 0) {
alphabet[string[i] - 'a'] = i + 1;
continue;
}
hasRepeated[string[i] - 'a'] = true;
alphabet[string[i] - 'a'] = i;
}
System.out.println(Arrays.toString(alphabet));
int min = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < alphabet.length; i++) {
if (hasRepeated[i]) {
if (alphabet[i] < min) {
min = alphabet[i];
minIndex = i;
}
}
}
return (char) (minIndex + 'a');
}
}