Question: DBOI, Recent Online Assessment Questions (MNIT Jaipur | 17th August 2023) | Count the Letters | Good Stings Selection
1
Entering edit mode

ADD COMMENTlink 14 months ago PoGo 2.4k
1
Entering edit mode

Solution :Count the letters

  import java.util.*;
public class Main
{
    public static int find(String S)
    {
        HashMap<Character,Integer> map=new HashMap<>();
        int count=0;
        for(int i=0;i<S.length();i++)
        {
            char ch=S.charAt(i);
             if(map.containsKey(ch))
               map.put(ch,map.get(ch)+1);
               else
               map.put(ch,1);
        }
          for(int x:map.values())
          {
              if(x==1)
                count++;
          }
          return count;
    }
    public static void main(String[] args) 
    {
        int max=Integer.MIN_VALUE;
        String ans="";
        String S[]={"once","three","sorry"};//Take input of string
        List<String> l=new ArrayList<>();
          for(String i:S)
            l.add(i);
            Collections.sort(l);
           HashMap<String,Integer> map=new HashMap<>();
             for(int i=0;i<l.size();i++)
             {
                 int  freq=find(l.get(i));
                   if(freq>max)
                   {
                       max=freq;
                   }
                   map.put(l.get(i),freq);
             }
             for(int i=0;i<l.size();i++)
             {
                 if(map.get(l.get(i))==max)
                 {
                   ans=l.get(i);
                     break;
                 }
             }
        System.out.println(ans);
            
        
    }
}

ADD COMMENTlink 14 months ago Itachi_Uchiha • 10
0
Entering edit mode
Q-2

ll dp[100001][4][(1<<6)-1] ;
ll cal(ll i,  ll count , ll mask , vll&v  , ll&n )
{

if(i==n)
return count==3; 

if(count==3)
return 1 ;

if(dp[i][count][mask]!=-1)
return dp[i][count][mask] ;

ll take=0 , nottake= 0 ;


if(count==0)
{
  // take 
      if(v[i]>0)
      take =cal(i+1, count+1, mask|v[i] , v , n ) ; 
      nottake = cal(i+1, count , mask ,v, n  ) ;
}
else
{

  // take
  if(mask&(v[i]))
  take = cal(i+1 , count+1 , mask&v[i] , v, n ) ; 
  nottake = cal(i+1, count, mask ,v ,n ) ; 

}

return dp[i][count][mask] = take+nottake ;

}


mll m; 
void solve()
{
ll n ;cin >>n ; 
vector<string>v(n); 
fl(i ,0 , n)cin >> v[i] ;

m['a']=0 , m['e']=1 , m['i']=2 , m['o']=3 ,m['u']=4 ; 

vll vv ; 
for(auto it: v) 
{
  string s  =it ; 
  ll num=0 ; 
  for(auto itr: s )
  {
    if(m.find(itr)!=m.end())
    {
      num+= (1<<m[itr]) ;
    }
  }
  vv.push_back(num);
}
memset(dp, -1 ,sizeof(dp)) ;
cout <<  cal(0 , 0 , 0 , vv ,  n  ) ;


}

 

ADD COMMENTlink 14 months ago Kshitiz Kumar • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts