-
Notifications
You must be signed in to change notification settings - Fork 1
/
Challenge03.java
60 lines (50 loc) · 1.56 KB
/
Challenge03.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
package challenge03;
import static org.junit.Assert.assertTrue;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
/**
* The Class Challenge03.
*
* Given a string, your code must find out the first repeated as well as
* non-repeated character in that string. For example, if “JavaConceptOfTheJay”
* is the given string, then ‘v’ is a first non-repeated character and ‘a’ is a
* first repeated character.
*/
public class Challenge03 {
public Character[] findNonRepatedAndRepeatedChar(String input) {
Character[] toReturn = new Character[2];
if (input == null || input.isEmpty()) {
return toReturn;
} else {
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (int i = 0; i < input.length(); i++) {
Integer oldValue = map.put(input.charAt(i), 0);
if (null != oldValue) {
map.put(input.charAt(i), oldValue + 1);
}
}
boolean nonRepeatedFound = false;
boolean repeatedFound = false;
for (Character c : map.keySet()) {
if (map.get(c) == 0 && !nonRepeatedFound) {
toReturn[0] = c;
nonRepeatedFound = true;
}
if (map.get(c) > 0 && !repeatedFound) {
toReturn[1] = c;
repeatedFound = true;
}
}
}
return toReturn;
}
@Test
public void test() {
Challenge03 tester = new Challenge03();
Character[] cs= tester.findNonRepatedAndRepeatedChar("avaConceptOftheDay");
System.out.println(cs[0] +" , " +cs[1]);
assertTrue(cs[0].equals('v'));
assertTrue(cs[1].equals('a'));
}
}