Fast Sort

From childhood we are taught that a comes before b then b comes before c and so on. So whenever we try to sort any given string we sort it in that manner only placing a before b and so on. But what happens if we initially change the pattern of sorting. This question arrived in Arav's young mind. He thought what would the final string be like if z comes before a and a comes after c and so on. He got really puzzled in finding out the final sorted string. So he asks you for help.

He gives you two strings. One the pattern string P which decides the order of alphabets and the second that is the final string F which needs to be sorted. Help him by telling him the final sorted string.

Example:

Input:  p = "wcyuogmlrdfphitxjakqvzbnes", s = "jcdokai"
Output: codijak

Approach

C++

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

string fastSort(string pstring s)
{

    int f[26] = {0};
    for (int i = 0i < s.size(); i++)
        f[s[i] - 'a']++;
    string str = "";
    for (int i = 0i < 26i++)
    {
        for (int j = 0j < f[p[i] - 'a']; j++)
            str += p[i];
    }
    return str;
}
int main()
{
    string p = "wcyuogmlrdfphitxjakqvzbnes";
    string s = "jcdokai";

    cout << fastSort(ps<< "\n";

    return 0;
}


No comments:

Post a Comment