forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (32 loc) · 955 Bytes
/
Solution.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
class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] cnt = new int[26];
for (int i = 0; i < licensePlate.length(); ++i) {
char c = licensePlate.charAt(i);
if (Character.isLetter(c)) {
cnt[Character.toLowerCase(c) - 'a']++;
}
}
String ans = "";
for (String w : words) {
if (!ans.isEmpty() && w.length() >= ans.length()) {
continue;
}
int[] t = new int[26];
for (int i = 0; i < w.length(); ++i) {
t[w.charAt(i) - 'a']++;
}
boolean ok = true;
for (int i = 0; i < 26; ++i) {
if (t[i] < cnt[i]) {
ok = false;
break;
}
}
if (ok) {
ans = w;
}
}
return ans;
}
}