CKS003 - Editorial

Dhritarashtra and Strings

Author: artha6391

Tester: anushka_nagar

Editorialist: murtuza19

DIFFICULTY:

SIMPLE

PREREQUISITES:

Understanding of Strings

PROBLEM:

Find the number of characters which occur in both given strings.

QUICK EXPLANATION:

For each string find the multiset of characters present in the string. The answer is the number of common elements in the two multisets. Since there are only 62 (26 uppercase alphabets + 26 lowercase alphabets + 10 digits) possible characters, the multiset of characters present in a string can be represented by an integer array of size 62.

EXPLANATION:

Since we must find the number of common characters present in both strings, the order can be any. For each string one needs to find the multiset of characters which appear in it. The intersection of the two multisets will be the longest pattern. Remember, in a multiset the same character may appear more than once.

SOLUTIONS:

Setter’s Solution

#include<bits/stdc++.h>

using namespace std;

int main()

{

int T;

cin>>T;

while(t–)

{

string A,B;

int arr[200]={0},count=0,i;

cin>>A>>B;

for(i=0;i<s.length();i++)

{

arr[A[i]]=arr[A[i]]+1;

}

for(i=0;i<B.length();i++)

{

if(arr[B[i]]>0)

{

count++;arr[B[i]]–;

}

}

cout<<count<<endl;

}

}

Tester’s Solution

#include<bits/stdc++.h>

using namespace std;

int main()

{

int T;

cin>>T;

while(t–)

{

string A,B;

int arr[200]={0},count=0,i;

cin>>A>>B;

for(i=0;i<s.length();i++)

{

arr[A[i]]=arr[A[i]]+1;

}

for(i=0;i<B.length();i++)

{

if(arr[B[i]]>0)

{

count++;arr[B[i]]–;

}

}

cout<<count<<endl;

}

}

Editorialist’s Solution

#include<bits/stdc++.h>

using namespace std;

int main()

{

int T;

cin>>T;

while(t–)

{

string A,B;

int arr[200]={0},count=0,i;

cin>>A>>B;

for(i=0;i<s.length();i++)

{

arr[A[i]]=arr[A[i]]+1;

}

for(i=0;i<B.length();i++)

{

if(arr[B[i]]>0)

{

count++;arr[B[i]]–;

}

}

cout<<count<<endl;

}

}