2287. Rearrange Characters to Make Target String
Description
You are given two 0-indexed strings s
and target
. You can take some letters from s
and rearrange them to form new strings.
Return the maximum number of copies of target
that can be formed by taking letters from s
and rearranging them.
Example 1:
Input: s = "ilovecodingonleetcode", target = "code" Output: 2 Explanation: For the first copy of "code", take the letters at indices 4, 5, 6, and 7. For the second copy of "code", take the letters at indices 17, 18, 19, and 20. The strings that are formed are "ecod" and "code" which can both be rearranged into "code". We can make at most two copies of "code", so we return 2.
Example 2:
Input: s = "abcba", target = "abc" Output: 1 Explanation: We can make one copy of "abc" by taking the letters at indices 0, 1, and 2. We can make at most one copy of "abc", so we return 1. Note that while there is an extra 'a' and 'b' at indices 3 and 4, we cannot reuse the letter 'c' at index 2, so we cannot make a second copy of "abc".
Example 3:
Input: s = "abbaccaddaeea", target = "aaaaa" Output: 1 Explanation: We can make one copy of "aaaaa" by taking the letters at indices 0, 3, 6, 9, and 12. We can make at most one copy of "aaaaa", so we return 1.
Constraints:
1 <= s.length <= 100
1 <= target.length <= 10
s
andtarget
consist of lowercase English letters.
Note: This question is the same as 1189: Maximum Number of Balloons.
Solutions
Solution 1: Counting
We count the occurrences of each character in the strings \(\textit{s}\) and \(\textit{target}\), denoted as \(\textit{cnt1}\) and \(\textit{cnt2}\). For each character in \(\textit{target}\), we calculate the number of times it appears in \(\textit{cnt1}\) divided by the number of times it appears in \(\textit{cnt2}\), and take the minimum value.
The time complexity is \(O(n + m)\), and the space complexity is \(O(|\Sigma|)\). Where \(n\) and \(m\) are the lengths of the strings \(\textit{s}\) and \(\textit{target}\), respectively. And \(|\Sigma|\) is the size of the character set, which is 26 in this problem.
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|