CC-18 : Filter Strings by given filter criteria
Description:
Given an array of Strings named words, filter all words that match all filter criteria substrings found in 2nd input array subsets and return the filtered Strings.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| words={"tree","free","be","red","he"}; subsets={"r","e"}; |
Matching words : tree,free,red |
| words={"radical","practical","logical"}; subsets={"cal"}; |
Matching words: radical,practical,logical |
| words={"radical","practical","logical"}; subsets={"calls"}; |
No matching words. |
Pseudocode:
| The java method should accept following input parameters: words (String array), subsets (String array). |
| Initialize a variable tempFreqMap of type HashMap |
| Initialize a variable substFreqMap of type HashMap |
| Iterate through subsets:
For each character in the Strings in subsets, find the maximum frequency of that char across subsets’s strings. Store the char and the max frequency found in substFreqMap.
|
| Initialize ArraysList of Strings named mtchWrdsAl to store word that match subsets. |
| Initialize a variable wordsFreqMap of type HashMap |
| Iterate through words :
For each character in the Strings in words, find the maximum frequency of that char across words ‘s Strings. Store the char and the max frequency found wordsFreqMap.
Iterate through keys of substFreqMap:
If frequency of characters part of substFreqMap is greater than frequency of same characters in wordsFreqMap, then the current word is a match and can be filtered. Add current word to mtchWrdsAl.
|
| After above loop completes, mtchWrdsAl contains filtered words that match subsets array. Return mtchWrdsAl.toArray(); |
Code:
public String[] hashMapWordsSubset(String[] words, String[] subsets) throws Exception{
HashMap< Character,Integer> tempFreqMap=new HashMap< Character, Integer>();
HashMap< Character,Integer> sbstChrsFreqMap=new HashMap< Character, Integer>();
String subset; char[] chars; int maxFreq;
for (int sIdx=0; sIdx < subsets.length; sIdx++) {
subset=subsets[sIdx];
chars=subset.toCharArray();
tempFreqMap.clear();
for (int cIdx=0; cIdx < chars.length; cIdx++) {
tempFreqMap.put(chars[cIdx], tempFreqMap.getOrDefault(chars[cIdx],0)+1);
maxFreq=Math.max(tempFreqMap.getOrDefault(chars[cIdx],0), sbstChrsFreqMap.getOrDefault(chars[cIdx],0));
sbstChrsFreqMap.put(chars[cIdx], maxFreq);
}
}
ArrayList< String> mtchWrdsAl=new ArrayList< String>();
String word;
HashMap< Character,Integer> wrdsFreqMap=new HashMap< Character, Integer>();
for (int wIdx=0; wIdx < words.length; wIdx++) {
word=words[wIdx];
chars=word.toCharArray();
wrdsFreqMap.clear();
boolean notSubset=false;char substrchr;
for (int cIdx=0; cIdx < chars.length; cIdx++) {
wrdsFreqMap.put(chars[cIdx], wrdsFreqMap.getOrDefault(chars[cIdx],0)+1);
}
notSubset=false;
Iterator itr=sbstChrsFreqMap.keySet().iterator();
while (itr.hasNext()) {
substrchr=(Character)itr.next();
if ( (sbstChrsFreqMap.get(substrchr) > wrdsFreqMap.getOrDefault(substrchr, 0))) {
notSubset=true;
break;
}
}
if (notSubset==false) {
mtchWrdsAl.add(word);
}
}
String[] mtchWrdsArr=new String[mtchWrdsAl.size()];
mtchWrdsAl.toArray(mtchWrdsArr);
return mtchWrdsArr;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |