Rearrange Characters to Make Target String - Problem

Imagine you have a collection of letter tiles from a word game, and you want to form as many copies of a specific target word as possible!

You are given two strings: s (your available letters) and target (the word you want to form). Your goal is to determine the maximum number of complete copies of the target string you can create by rearranging the letters from s.

Key Rules:

  • You can use each letter from s only once
  • You can rearrange letters in any order
  • You must form complete copies of the target string

Example: If s = "ilovecodingsomuchthatticouldcodeallday" and target = "code", you need to count how many times you can spell "code" using the available letters.

Input & Output

example_1.py — Basic Case
$ Input: s = "ilovecodingsomuchthatticouldcodeallday", target = "code"
Output: 2
💡 Note: We can form "code" twice. Available letters: i(3), l(3), o(4), v(1), e(2), c(4), d(4), n(1), g(1), s(1), m(1), u(4), h(3), t(5), a(4), y(1). For "code" we need: c(1), o(1), d(1), e(1). Ratios: c(4/1=4), o(4/1=4), d(4/1=4), e(2/1=2). Minimum is 2.
example_2.py — No Possible Formation
$ Input: s = "abcdefg", target = "code"
Output: 0
💡 Note: We cannot form "code" even once because we're missing the letter 'o' in the source string. When a required letter is missing, the result is 0.
example_3.py — Single Character Target
$ Input: s = "aaaaaa", target = "a"
Output: 6
💡 Note: Simple case where target is a single character. We have 6 'a's available and need 1 'a' per target, so we can form 6 copies of the target "a".

Visualization

Tap to expand
🎯 Letter Tiles Game Strategy📦 Your Available TilesCCODDEE+ other letters...🎯 Target Word: "CODE"Need per word: C(1) + O(1) + D(1) + E(1)CODE🧮 Calculate RatiosC: 2 available ÷ 1 needed = 2O: 1 available ÷ 1 needed = 1 ← Bottleneck!D: 2 ÷ 1 = 2, E: 2 ÷ 1 = 2✨ Result: Can Form 1 Complete "CODE"First word: Use C, O, D, E (leaves C, D, E available)Second word: Need C, O, D, E but O is missing!The letter O is the limiting factor⚡ Algorithm: O(n + m) Time, O(k) Space1. Count frequencies in both strings2. For each target character: calculate available ÷ needed3. Return minimum ratio4. This finds the bottleneck character!
Understanding the Visualization
1
Count Your Tiles
First, count how many of each letter you have available
2
Check Requirements
Count how many of each letter you need for one target word
3
Find the Bottleneck
Calculate available÷needed for each letter - the smallest ratio is your answer
4
Verify Result
The limiting character determines maximum possible complete words
Key Takeaway
🎯 Key Insight: The character with the smallest available-to-needed ratio determines how many complete target strings you can form. This bottleneck approach gives us the optimal O(n + m) solution!

Time & Space Complexity

Time Complexity
⏱️
O(n + m)

Where n is length of s and m is length of target. We iterate through target once to count, then through s once to calculate.

n
2n
Linear Growth
Space Complexity
O(k)

Where k is the number of unique characters in target (at most 26 for English alphabet).

n
2n
Linear Space

Constraints

  • 1 ≤ s.length ≤ 100
  • 1 ≤ target.length ≤ 10
  • s and target consist of lowercase English letters only
  • target.length ≤ s.length (otherwise answer would always be 0)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.6K Views
Medium Frequency
~15 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen