Amazon Oa question 2023

image
image

Brute Force :-
Find cnt of string t in string s until we are getting string t in string s; if any char of t doesn’t present in s at any moment then we will break & return the cnt; & if string s is empty at any moment then again we will break from the loop & return the cnt.

Better Approach :-
Cases :-
S = abcbacbbac t = abc → unique

S = abcbacbbac t = abbc → duplicate

S = abcbacbbac t = abcp

By analyzing the above 3 cases, the approach is →

Take 2 hashmaps. One will store the cnt of char present in s & another will store the cnt of char present in t.

Now traverse t & ans = min(ans, mp1[t[i]] / mp2[t2[i]] )

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {

string s, t; 

cin>>s>>t;


unordered_map<char, ll> mp1, mp2; // for s & t

for(int i=0; i<s.size(); i++)
{
    mp1[s[i]]++;
}

for(int i=0; i<t.size(); i++)
{
     mp2[t[i]]++; 
}

ll result=1e9; 

for(int i=0; i<t.size(); i++) 
{
    if(mp1.find(t[i])==mp1.end()) 
    {
        // it means t[i] is not present in s hence we will not get any subset 
        return 0; 
    }
    
    ll val =  mp1[t[i]]/mp2[t[i]];
    
    
    cnt = min(cnt, val); 
}
cout<<cnt<<" ";
return 0;

}

Comments (0)