Word Subsets | Leetcode 916

preview_player
Показать описание
This video explains word subsets problem using the the most optimal hashmap approach.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
---------------------------------------------------------------------------------------------------------------------------------------------------------------

Рекомендации по теме
Комментарии
Автор

Very nicely explained, thank you sir!

divyanshsharma
Автор

what is the reasoning for the usage of the array to keep track of the number of instances of the characters? The same can be accomplished using maps right?

bhavukkalra
Автор

Thank you sir for the solution. I did it with hashmap . This is my code:-class Solution {
public static Map<Character, Integer>getfrequencyMap(String str)
{
Map<Character, Integer> frequencyMap=new HashMap<>();
for(char c:str.toCharArray())
{
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0)+1);
}

return frequencyMap;
}
public List<String> wordSubsets(String[] words1, String[] words2) {
List<String> result=new ArrayList<>();
Map<Character, Integer> subset=new HashMap<>();
for(String word:words2)
{
Map<Character, Integer> part=getfrequencyMap(word);
for(Map.Entry<Character, Integer> entry:part.entrySet())
{
char key=entry.getKey();
int count=entry.getValue();
subset.put(key, Math.max(subset.getOrDefault(key, 0), count));
}

}

for(String Word:words1)
{
Map<Character, Integer>
int sum=0;
for(Map.Entry<Character, Integer> entry:subset.entrySet())
{
char key=entry.getKey();
int count=entry.getValue();
if(wordFreqMap.getOrDefault(key, 0)<count)
{
sum++;
}

}
if(sum==0)
{
result.add(Word);
}
} return result;

}

}

divyasingh
Автор

I think we can do word2 operations on a single map also

rakshitshandilya
Автор

first week is always full of easy questions. Difficult question ana chalu hone hi wala hai

Anikait-hd