LeetCode solutions

2372. Rearrange Characters to Make Target String

My accepted Python solution to LeetCode problem 2372, Rearrange Characters to Make Target String, running in 3ms.

  • Difficulty: Easy
  • Python
  • Runtime 3ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.3MB, accepted 2026-01-01.

python
class Solution:
    def rearrangeCharacters(self, s: str, target: str) -> int:
        from collections import Counter
        s_count = Counter(s)
        t_count = Counter(target)
        result = float('inf')
        for char, count in t_count.items():
            if char in s_count:
                result = min(result, s_count[char] // count)
            else:
                return 0
        return result

Source