[LeetCode] Rearrange Characters to Make Target String

2287. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int rearrangeCharacters(string s, string target) {
unordered_map<char, int> mp;
unordered_map<char, int> mp2;
for(auto& ch : s) mp[ch]++;
for(auto& ch : target) mp2[ch]++;
int res = INT_MAX;
for(auto [k, v] : mp2) {
res = min(res, mp[k] / v);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/29/PS/LeetCode/rearrange-characters-to-make-target-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.