1
Approach :
2
=> Take two map, one to store frequency of target, and another for sentence.
3
=> Traverse over the mp(frequency of target ) and calculate the minimum frequency ratio
4
mn = min(mn , frequency of a char in sentance / frequency of same char in target) ;
5

6
Space : O(n) Time : O(n) class Solution {
7
public:
8
int rearrangeCharacters(string s, string target) {
9
unordered_map<char, int> targetFreq;
10
for (auto a : target) {
11
targetFreq[a]++;
12
}
13
unordered_map<char, int> sentFreq;
14
for (auto a : s) {
15
sentFreq[a]++;
16
}
17
int mn = INT_MAX;
18
for (auto a : targetFreq) {
19
mn = min(mn, sentFreq[a.first] / a.second);
20
}
21
return mn;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0