CPRMT - Common Permutation SPOJ editorial - UCS - Unleash-Coding-Skills

Friday, 4 May 2018

CPRMT - Common Permutation SPOJ editorial - UCS

The link for this problem is CPRMT - Common Permutation :

http://www.spoj.com/problems/CPRMT/

The Solution to this problem  CPRMT - Common Permutation we are using is best one.

Solution:
For Each pair of Input Strings, we can take a help of two counter array which counts the number of times each character appears in the string.

So then we can print the each character from a to z :
 a - 0th index in Arrays
b - 1st index in Arrays
c - 2ne index in Arrays
..
..
The corresponding location minimum count is taken and the corresponding character is printed that many numbers of times.

The CPP solution for  CPRMT - Common Permutation :

#include<bits/stdc++.h>
using namespace std;

int main(){
    string a,b;
    while(    cin >> a ){
        cin>> b;
        int cntA[26], cntB[26];
        memset(cntA,0,sizeof(cntA));
        memset(cntB,0,sizeof(cntB));
        for(int i=0;i<a.size();i++) cntA[a[i]-'a']++;
        for(int i=0;i<b.size();i++) cntB[b[i]-'a']++;
        for(int i=0;i<26;i++){
          if(cntA[i] > 0 && cntB[i] > 0){
                int s = min(cntA[i],cntB[i]);
                for(int j=0;j<s;j++) cout<<(char)('a'+i);
            }
        }
        cout << endl;
    }
    return 0;
}


Happy Coding.......

No comments:

Post a comment