1
class Solution {
2
public String shortestCompletingWord(String licensePlate, String[] words) {
3
// Store count of letters in LicensePlate
4
int[] licensePlateCount = new int[26];
5

6
// To store all words which meet the criteria
7
ArrayList<String> res = new ArrayList<>();
8
// To find min length word that meets the criteria
9
int min = Integer.MAX_VALUE;
10

11
// Add char count for each char in LicensePlate
12
for (Character c : licensePlate.toCharArray()) {
13
if (isChar(c)) {
14
licensePlateCount[Character.toLowerCase(c) - 'a']++;
15
}
16
}
17

18
// Add char count for each word in words
19
for (String word : words) {
20
int[] wordCharCount = new int[26];
21
boolean flag = true;
22

23
for (Character c : word.toCharArray()) {
24
wordCharCount[Character.toLowerCase(c) - 'a']++;
25
}
26

27
// Eliminate words that don't satisfy the criteria
28
for (int i = 0; i < 26; i++) {
29
if (licensePlateCount[i] > wordCharCount[i]) flag = false;
30
}
31

32
// Add words satisfying criteria to res and calculate min word length
33
if (flag) {
34
res.add(word);
35
if (word.length() < min) min = word.length();
36
}
37
}
38

39
// Return 1st word in array meeting all criteria
40
for (int i = 0; i < res.size(); i++) {
41
if (res.get(i).length() == min) return res.get(i);
42
}
43

44
// If not found, return -1 (or whatever interviewer expects)
45
return "-1";
46
}
47

48
private boolean isChar(Character c) {
49
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true;
50

51
return false;
52
}
53
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0