Skip to content
LC-2287 Easy LeetCode

2287. Rearrange Characters to Make Target String

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 60% Topics: Hash Table, String, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n + m)
# Space: O(1)

import collections


# freq table
class Solution(object):
    def rearrangeCharacters(self, s, target):
        """
        :type s: str
        :type target: str
        :rtype: int
        """
        cnt1 = collections.Counter(s)
        cnt2 = collections.Counter(target)
        return min(cnt1[k]//v for k, v in cnt2.iteritems())

Solution from kamyu104/LeetCode-Solutions · MIT