2
public String shortestCompletingWord(String licensePlate, String[] words) {3
// Store count of letters in LicensePlate4
int[] licensePlateCount = new int[26];6
// To store all words which meet the criteria7
ArrayList<String> res = new ArrayList<>();8
// To find min length word that meets the criteria9
int min = Integer.MAX_VALUE;11
// Add char count for each char in LicensePlate12
for (Character c : licensePlate.toCharArray()) {14
licensePlateCount[Character.toLowerCase(c) - 'a']++;18
// Add char count for each word in words19
for (String word : words) {20
int[] wordCharCount = new int[26];23
for (Character c : word.toCharArray()) {24
wordCharCount[Character.toLowerCase(c) - 'a']++;27
// Eliminate words that don't satisfy the criteria28
for (int i = 0; i < 26; i++) {29
if (licensePlateCount[i] > wordCharCount[i]) flag = false;32
// Add words satisfying criteria to res and calculate min word length35
if (word.length() < min) min = word.length();39
// Return 1st word in array meeting all criteria40
for (int i = 0; i < res.size(); i++) {41
if (res.get(i).length() == min) return res.get(i);44
// If not found, return -1 (or whatever interviewer expects)48
private boolean isChar(Character c) {49
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true;