숫자 짝꿍

Lv. 1

Solution

class Counter {
  private readonly map: Map<string, number>;
 
  constructor(str: string) {
    this.map = new Map();
    for (const char of str) {
      this.add(char);
    }
  }
 
  get(key: string) {
    return this.map.get(key) || 0;
  }
 
  add(key: string) {
    this.map.set(key, this.get(key) + 1);
  }
}
 
export function partnerNumber(X: string, Y: string) {
  const counterX = new Counter(X);
  const counterY = new Counter(Y);
 
  const getCount = (num: number) => {
    return Math.min(counterX.get(num.toString()), counterY.get(num.toString()));
  };
 
  const answer = new Array(10)
    .fill(undefined)
    .reduce((prev, _, num) => num.toString().repeat(getCount(num)) + prev, '');
 
  if (answer === '') {
    return '-1';
  }
 
  if (getCount(0) === answer.length) {
    return '0';
  }
 
  return answer;
}