-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question159.java
33 lines (26 loc) · 990 Bytes
/
Question159.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
/*
Given a string, return the first recurring character in it, or null if there is no recurring character.
For example, given the string "acbbac", return "b". Given the string "abcdef", return null.
*/
import java.util.ArrayList;
public class Question159 {
public static void main(String[] args) {
System.out.println(firstRecurringChar("acbbac"));
System.out.println(firstRecurringChar("abcdef"));
}
// Solve with O(n) time and space
public static Character firstRecurringChar(String str) {
ArrayList<Character> foundChars = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
char currentChar = str.charAt(i);
// if key does not exist, add it in
if (!foundChars.contains(currentChar)) {
foundChars.add(currentChar);
} else {
// found a recurring character
return currentChar;
}
}
return null;
}
}