Straightforward, but so many things can go wrong here. It’s important to clarify the requirement beforehand.
class Solution { public String shortestCompletingWord(String licensePlate, String[] words) { int[] dict = new int[256]; int size = 0; for (char c : licensePlate.toCharArray()) { if (Character.isLetter(c)) { dict[Character.toLowerCase(c)]++; size++; } } String res = ""; for (String w : words) { if (w.length() < size) continue; int[] tmp = Arrays.copyOf(dict, dict.length); int cnt = 0; for (char c : w.toCharArray()) { if (tmp[c] > 0) cnt++; tmp[c]--; } if (cnt == size && (res.isEmpty() || w.length() < res.length())) { res = w; } } return res; } }