{"size": 1056, "ext": "js", "max_stars_repo_path": "791-custom-sort-string.js", "max_stars_repo_name": "consumer-tasks/letcode", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "791-custom-sort-string.js", "max_issues_repo_name": "consumer-tasks/letcode", "max_issues_repo_issues_event_min_datetime": "2021-05-02T10:59:18.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-14T10:02:22.000Z", "max_forks_repo_path": "791-custom-sort-string.js", "max_forks_repo_name": "consumer-tasks/letcode", "max_forks_repo_forks_event_min_datetime": "2021-11-26T20:43:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-26T20:43:19.000Z"}
{"max_stars_count": null, "max_issues_count": 25, "max_forks_count": 1, "avg_line_length": 22.4680851064, "max_line_length": 106, "alphanum_fraction": 0.5833333333}
/**
 * @param {string} S
 * @param {string} T
 * @return {string}
 */
const customSortString = function(S, T) {
  const arr = [];
  const remaining = [];
  const hash = {};
  for (let i = 0; i < S.length; i++) {
    if (T.indexOf(S.charAt(i)) !== -1) {
      arr.push(S.charAt(i));
    }
  }
  let char;
  for (let j = 0; j < T.length; j++) {
    char = T.charAt(j);
    if (arr.indexOf(char) === -1 && remaining.indexOf(char) === -1) {
      remaining.push(char);
    }
    hash[char] = hash.hasOwnProperty(char) ? hash[char] + 1 : 1;
  }
  return `${genPart(arr, hash)}${genPart(remaining, hash)}`;
};

function constructStr(char, num) {
  let str = "";
  for (let i = 0; i < num; i++) {
    str += char;
  }
  return str;
}

function genPart(arr, hash) {
  return arr.reduce((ac, el) => {
    return ac + constructStr(el, hash[el]);
  }, "");
}

console.log(customSortString("kqep", "pekeq"));
console.log(
  customSortString(
    "hucw",
    "utzoampdgkalexslxoqfkdjoczajxtuhqyxvlfatmptqdsochtdzgypsfkgqwbgqbcamdqnqztaqhqanirikahtmalzqjjxtqfnh"
  )
);
