-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortestWordInCarPlate.java
48 lines (42 loc) · 1.42 KB
/
ShortestWordInCarPlate.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
import java.util.List;
public class ShortestWordInCarPlate {
List<String> vocabulary;
ShortestWordInCarPlate(List<String> vocabulary) {
this.vocabulary = vocabulary;
}
public String findShortestWord(String car_plate) {
System.out.println(car_plate);
StringBuilder text = new StringBuilder();
int counter = 0;
for (int i = 0; i < car_plate.length(); i++) {
if (Character.isLetter(car_plate.charAt(i))) {
text.append(car_plate.charAt(i));
}
}
String newText = text.toString();
String low = newText.toLowerCase();
String up = newText.toUpperCase();
int vocabIterator = 0;
String minString = "";
while (vocabIterator < this.vocabulary.size()) {
boolean check = true;
for (int i = 0; i < newText.length(); i++) {
check &=
((this.vocabulary.get(vocabIterator).indexOf(low.charAt(i)) != -1)
|| (this.vocabulary.get(vocabIterator).indexOf(up.charAt(i)) != -1));
}
if (check
&& (minString.equals("")
|| this.vocabulary.get(vocabIterator).length() < minString.length())) {
minString = this.vocabulary.get(vocabIterator);
if (minString.length() == newText.length()) {
System.out.println(minString);
return minString;
}
}
vocabIterator++;
}
System.out.println(minString);
return minString;
}
}