2287. Rearrange Characters to Make Target String LeetCode Solution
In this guide, you will get 2287. Rearrange Characters to Make Target String LeetCode Solution with the best time and space complexity. The solution to Rearrange Characters to Make Target String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Rearrange Characters to Make Target String solution in C++
- Rearrange Characters to Make Target String solution in Java
- Rearrange Characters to Make Target String solution in Python
- Additional Resources
Problem Statement of Rearrange Characters to Make Target String
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 and target consist of lowercase English letters.
Note: This question is the same as 1189: Maximum Number of Balloons.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(128) = O(1)
2287. Rearrange Characters to Make Target String LeetCode Solution in C++
class Solution {
public:
int rearrangeCharacters(string s, string target) {
int ans = s.length();
vector<int> countS(128);
vector<int> countT(128);
for (const char c : s)
++countS[c];
for (const char c : target)
++countT[c];
for (const char c : target)
ans = min(ans, countS[c] / countT[c]);
return ans;
}
};
/* code provided by PROGIEZ */
2287. Rearrange Characters to Make Target String LeetCode Solution in Java
class Solution {
public int rearrangeCharacters(String s, String target) {
int ans = s.length();
int[] countS = new int[128];
int[] countT = new int[128];
for (final char c : s.toCharArray())
++countS[c];
for (final char c : target.toCharArray())
++countT[c];
for (final char c : target.toCharArray())
ans = Math.min(ans, countS[c] / countT[c]);
return ans;
}
}
// code provided by PROGIEZ
2287. Rearrange Characters to Make Target String LeetCode Solution in Python
class Solution:
def rearrangeCharacters(self, s: str, target: str) -> int:
countS = collections.Counter(s)
countT = collections.Counter(target)
return min(countS[c] // countT[c] for c in target)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.