Minimum Number of Pushes to Type Word II

MediumHash TableStringGreedySortingCounting

Solution

export function minimumPushes(word: string): number {
  const charCounter = new Map<string, number>();
  for (const char of word) {
    charCounter.set(char, (charCounter.get(char) ?? 0) + 1);
  }
  const counts = [...charCounter.values()].sort((a, b) => b - a);
  return counts.reduce((prev, count, i) => prev + (Math.floor(i / 8) + 1) * count, 0);
}

Complexity

  • Time: O(N)
  • Space: O(1)
  • charCounter의 최대 크기는 알파벳의 개수 26개이므로, 복잡도에 영항을 주지 않는다.