-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringArrayUniqueElementsCount.java
More file actions
67 lines (58 loc) · 2.12 KB
/
StringArrayUniqueElementsCount.java
File metadata and controls
67 lines (58 loc) · 2.12 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
60
61
62
63
64
65
66
67
package com.javaprep.core;
/**
*
* It is well known that launch codes work both forward and reverse,
* meaning that code "123" is effectively the same as code "321", a major
* security flaw which we have top people working to correct. Someone has stolen
* an extremely long list of launch codes, and you want to see how many
* "effective" codes were stolen, so that you know what codes to replace.
*
* Determine how many unique codes are contained in an array of nuclear launch
* codes.
*
* Please report the big-O runtime complexity of your solution
*
*
*/
public class StringArrayUniqueElementsCount {
public static int getNumUniqueCodes(String[] codes) {
Set<String> codeSet = new HashSet<String>();
for (String code : codes) {
StringBuffer revCode = new StringBuffer(code).reverse();
if (code != null && !codeSet.contains(revCode.toString())) {
codeSet.add(code);
}
}
return codeSet.size();
}
public static void main(String[] args) {
String[][] tests = { { "xy", "yx" }, // expected 1
{ "xy", "yx", "asdf", "fdsa" }, // expected 2
{ "x", "x" }, // expected 1
{ "xy", "yx", "asdf", "fdsa", "qwer", "yxy", "yxy" }, // expected
// 4
{ "xy", "yx", "xy", "xy", "asdf", "fdsa", "qwer", "yxy", "yxy" }, // expected
// 4
{ "" }, // expected 1
{}, // expected 0
{ "xy" }, // expected 1
{ "xy", "yxz" }, // expected 2
{ " abc", " cba" }, // expected 2
{ "abc", "xyz", "abc", "123", "zyx" }, // expected 3
{ "abcd", "dbca" } // expected 2
};
int[] expected = { 1, 2, 1, 4, 4, 1, 0, 1, 2, 2, 3, 2 };
boolean allTestPassed = true;
for (int i = 0; i < tests.length; i++) {
int numDetected = getNumUniqueCodes(tests[i]);
if (numDetected != expected[i]) {
allTestPassed = false;
System.out.println("Incorrect number of codes for test case " + i + ". Expected " + expected[i]
+ ", but detected " + numDetected);
}
}
if (allTestPassed) {
System.out.println("All Tests Pass!");
}
}
}