-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL3.java
More file actions
72 lines (60 loc) · 1.88 KB
/
Copy pathL3.java
File metadata and controls
72 lines (60 loc) · 1.88 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
68
69
70
71
72
import java.util.ArrayList;
class L3 {
public static int countKeywords(String keywords) {
if (keywords.isEmpty()) {
return 0;
}
int Length = 0;
int Count = 0;
String[] List = keywords.split(";");
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> checked = new ArrayList<>();
boolean alreadyChecked = false;
for (int i = 0; i < List.length; i++) {
words.add(List[i].trim());
}
for (int i = 0; i < words.size(); i++) {
for (int k = 0; k < checked.size(); k++) {
if (words.get(i).equalsIgnoreCase(checked.get(k))) {
alreadyChecked = true;
break;
}
}
if (alreadyChecked == true) {
continue;
}
else if (!words.get(i).isEmpty()) {
checked.add(words.get(i));
for (int j = 0; j < words.size(); j++) {
if (words.get(i).equalsIgnoreCase(words.get(j)) && Count == 0) {
Length++;
break;
}
}
}
}
return Length;
}
public static void main(String[] args) {
// Test 1
String test1 = "";
int result = countKeywords(test1);
System.out.println("Test 1 result: " + result); // expect 0
// Test 2
String test2 = "fruit; vegetables; drinks";
result = countKeywords(test2);
System.out.println("Test 2 result: " + result); // expect 3
// Test 3
String test3 = "flat white; latte; cappuccino; cafe au lait; mocha";
result = countKeywords(test3);
System.out.println("Test 3 result: " + result); // expect 5
// Test 4
String test4 = "oolong; ; herbal; ;; ; matcha; matcha";
result = countKeywords(test4);
System.out.println("Test 4 result: " + result); // expect 3
// Test 5
String test5 = ";hoki;;h//;;; ; ik ; ; ;i;;";
result = countKeywords(test5);
System.out.println("Test 5 result: " + result); // expect 4
}
}