2287. Rearrange Characters to Make Target String

  • Hash Table
  • String
  • Counting
TIME
O(|s| + |target|)
SPACE
O(1)
problem on leetcode ↗source on github ↗

Intuition

Each copy of target consumes a fixed number of each of its letters. So the number of copies we can build is limited by whichever target letter runs out first in s. Count both strings, then take the bottleneck.

Approach

  1. Count the frequency of each character in s and in target.
  2. For each distinct character ch in target, s can support count_s[ch] // count_target[ch] copies.
  3. The answer is the minimum of those ratios over all distinct characters of target.

Complexity

  • Time: O(|s| + |target|), counting both strings
  • Space: O(1), fixed-size frequency maps (26 letters)

Edge Cases

  • A target letter absent from s -> ratio is 0, answer 0
  • Repeated target letters (e.g. "aaaaa") -> integer division by the per-copy count handles it
  • s shorter than target -> some ratio is 0
  • Extra letters in s not in target -> ignored

Code

from collections import Counter


class Solution:
    def rearrangeCharacters(self, s: str, target: str) -> int:
        """
        2287. Rearrange Characters to Make Target String
        Time: O(|s| + |target|)
        Space: O(1)
        """
        s_counts = Counter(s)
        target_counts = Counter(target)

        return min(s_counts[ch] // need for ch, need in target_counts.items())