-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_748.java
41 lines (38 loc) · 1.23 KB
/
_748.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
package com.fishercoder.solutions.firstthousand;
public class _748 {
public static class Solution1 {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] counts = new int[26];
for (char c : licensePlate.toCharArray()) {
if (Character.isAlphabetic(c)) {
counts[Character.toLowerCase(c) - 'a']++;
}
}
String result = "";
for (String word : words) {
if (isComplete(word, counts)) {
if (result.equals("")) {
result = word;
} else if (word.length() < result.length()) {
result = word;
}
}
}
return result;
}
private boolean isComplete(String word, int[] counts) {
int[] tmp = counts.clone();
for (char c : word.toCharArray()) {
if (tmp[c - 'a'] > 0) {
tmp[c - 'a']--;
}
}
for (int i : tmp) {
if (i != 0) {
return false;
}
}
return true;
}
}
}