Skip to content

Commit b6193ae

Browse files
authored
Create rearrange-characters-to-make-target-string.cpp
1 parent b31e371 commit b6193ae

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(n + m)
2+
// Space: O(1)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
int rearrangeCharacters(string s, string target) {
8+
vector<int> cnt1(26), cnt2(26);
9+
for (const auto& c : s) {
10+
++cnt1[c - 'a'];
11+
}
12+
for (const auto& c : target) {
13+
++cnt2[c - 'a'];
14+
}
15+
int result = numeric_limits<int>::max();
16+
for (int i = 0; i < size(cnt2); ++i) {
17+
if (!cnt2[i]) {
18+
continue;
19+
}
20+
result = min(result, cnt1[i] / cnt2[i]);
21+
}
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)