Answer to Question #180577 in Java | JSP | JSF for Ritesh

Question #180577

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


1
Expert's answer
2021-04-13T22:41:21-0400
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);
   }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
14.04.21, 14:34

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!

Ritesh
14.04.21, 08:58

Thank you so much for you kind help , Thank you.

Leave a comment

LATEST TUTORIALS
New on Blog