Help me in solving CHEFING problem

My issue

My code

#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
	// your code goes here
	ll int t;
	std::cin >> t;
	while(t--)
	{
	    ll int n;
	    cin>>n;
	    string a;
	    cin>>a;
	    for(ll int i=1;i<n;i++)
	    {
	        string s; string str;
	        cin>>s;
	        for(ll int p=0;p<a.size();p++)
	        {
	            for(ll int q=0;q<s.size();q++)
	            {
	                if(a[p]==s[q])
	                {
	                    str+=s[q];
	                    s.erase(s.begin()+q, s.begin()+q+1);
	                    break;
	                }
	            }
	        }
	        a=str;
	    }
	    std::cout << a.size() << std::endl;
	}
	return 0;
}

Problem Link: CHEFING Problem - CodeChef

@pranto1610
i don’t get your logic
its a simple problem u have to check for every lower case letter from a to z if any character is present in all the string then it should be included in your answer.Thus u have to return the total count of such character.
This is my code i have written in much simpler way with same logic hope u will get it.
include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
string s[n];
int ans=0;
for(int i=0;i<n;i++)
{
cin>>s[i];
}
for(char ch=β€˜a’;ch<=β€˜z’;ch++)
{
int cnt=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<s[i].size();j++)
{
if(s[i][j]==ch)
{
cnt++;
break;
}
}
}
if(cnt==n)
ans++;
}
cout<<ans<<endl;
}
return 0;
}

1 Like

Thanks from Bangladesh for your kind replay .

1 Like