Leetcode in Java && Oracle
421 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-12-10_00-32-27.png
75.3 KB
#N748. Shortest Completing Word
problem link

#solution
Map<Character, Integer> map=new HashMap<>();
int minLength=Integer.MAX_VALUE;
String result = null;
for(char ch: licensePlate.toLowerCase().toCharArray())
if(Character.isLetter(ch))map.put(ch, map.getOrDefault(ch, 0) +1);
for(String word:words){
System.out.println(word);
Map<Character, Integer> temp = new HashMap<>(map);
int count = temp.size();
for(char ch:word.toCharArray())
if(temp.containsKey(ch))temp.put(ch, temp.get(ch) -1);

for(Map.Entry<Character, Integer> entry: temp.entrySet()){
if(entry.getValue()>0) break;
count--;
}
if(count<=0&&minLength>word.length()){
result=word;
minLength=word.length();
}
}
return result;