Input
aaaammmmyyyy
aamy am amy ammmy
output
3
Explanation: Line 1(Red font) - Represents stretched word NextLine(Black font) -List of Query words Explanation: The Stretched string contains a’s - 4, m’s - 4, y’s - 4 count of all alphabets in sequence > 3 So the query words should contain all the alphabets with count of each one is at least 1 aamy amy ammmy - are the words which satisfies the given criteria. am - ‘y’ is missing
Hence output - 3
import java.util.*;
public class Main
{
static Map<Character,Integer> frequency(String st)
{
Map<Character,Integer> map=new LinkedHashMap<>();
for(int i=0;i<st.length();i++)
{
if(map.containsKey(st.charAt(i)))
{
map.put(st.charAt(i),map.get(st.charAt(i))+1);
}
else
{
map.put(st.charAt(i),1);
}
}
return map;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
String query_w[]=sc.nextLine().split(" ");
Map<Character,Integer> map=frequency(st);
int count=0;
boolean flag=true;
for(int i=0;i<query_w.length;i++){
flag=true;
String word=query_w[i];
Map<Character,Integer> map1=frequency(word);
if(map1.size()!=map.size())
continue;
for (Character ch : map.keySet()){
if(!map1.containsKey(ch))
{
flag=false;
break;
}
else if(map1.get(ch)>map.get(ch))
flag=false;
break;
}
if(flag)
count++;
}
System.out.println(count);
}
}
Comments
Dear Ritesh, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!
Thank you so much for you kind help , Thank you.
Leave a comment