You are given two strings order and s. All the characters of order are unique and were sorted in some custom order previously.

Permute the characters of s so that they match the order that order was sorted. More specifically, if a character x occurs before a character y in order, then x should occur before y in the permuted string.

Return any permutation of s that satisfies this property.

&nbsp;
Example 1:

Input: order = "cba", s = "abcd"
Output: "cbad"
Explanation: 
"a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a". 
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.


Example 2:

Input: order = "cbafg", s = "abcd"
Output: "cbad"


&nbsp;
Constraints:


	1 &lt;= order.length &lt;= 26
	1 &lt;= s.length &lt;= 200
	order and s consist of lowercase English letters.
	All the characters of order are unique.


class Solution:
    def customSortString(self, order: str, s: str) -> str:
        charValue = [0] * 26
        for i in range(len(order)):
            idx = ord(order[i]) - ord('a')
            charValue[idx] = 26 - i

        arrS = []
        n = 0
        for c in s:
            arrS.append(c)
            n += 1

        sorted = False
        while not sorted:
            sorted = True
            for i in range(n - 1):
                if charValue[ord(arrS[i]) - ord('a')] < charValue[ord(arrS[i + 1]) - ord('a')]:
                    sorted = False
                    arrS[i], arrS[i + 1] = arrS[i + 1], arrS[i]

        return ''.join(arrS)
class Solution {
    public String customSortString(String order, String s) {
      if(s.length() <= 1) return s;
      
      StringBuilder finalString = new StringBuilder();
      HashMap<Character, Integer> hm = new HashMap<>();
      
      for(int i = 0; i < s.length(); i++) {
        char actualChar = s.charAt(i);
        
        if(order.indexOf(actualChar) == -1) {
          finalString.append(actualChar);
        } else {
          hm.put(actualChar, hm.getOrDefault(actualChar, 0) + 1);
        }
      }
      
      for(int i = 0; i < order.length(); i++) {
        char actualChar = order.charAt(i);
        
        if (hm.get(actualChar) != null){
          for(int j = 0; j < hm.get(actualChar); j++) {
            finalString.append(actualChar);
          }
        }
      }
      
      return finalString.toString(); 
    }
}
class Solution {
public:
    string customSortString(string order, string s) {

        map<char,int>mp;

        for(int i =0 ; i<order.size() ; i++)
            mp[order[i]]=i+1;

        vector<pair<int,char> > p;
        int x=200;
        for(int i =0 ; i < s.size(); i++)
        {
            if(mp[s[i]]!=0)
            p.push_back(make_pair(mp[s[i]],s[i]));
            else
            p.push_back(make_pair(x--,s[i]));

        }

        sort(p.begin(),p.end());
        string ans="";
        for(int i =0 ;i < p.size(); i++)
        {
            ans+=p[i].second;
        }
        return ans;
    }
};
var customSortString = function(order, s) {
    const hm = new Map();
    for(let c of s) {
        if(!hm.has(c)) hm.set(c, 0);
        hm.set(c, hm.get(c) + 1);
    }
    
    let op = "";
    for(let c of order) {
        if(hm.has(c)) {
            op += "".padStart(hm.get(c), c);
            hm.delete(c);
        }
    }
    for(let [c, occ] of hm) {
        op += "".padStart(hm.get(c), c);
        hm.delete(c);
    }
    return op;
};
Custom Sort String