Skip to content

Commit 96c14da

Browse files
authored
Create minimum-number-of-steps-to-make-two-strings-anagram-ii.cpp
1 parent 061b3cf commit 96c14da

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
int minSteps(string s, string t) {
8+
const auto& count1 = counter(s);
9+
const auto& count2 = counter(t);
10+
int result = 0;
11+
for (int i = 0; i < count1.size(); ++i) {
12+
result += abs(count1[i] - count2[i]);
13+
}
14+
return result;
15+
}
16+
17+
private:
18+
vector<int> counter(const string& s) const {
19+
vector<int> count(26);
20+
for (const auto& c : s) {
21+
++count[c - 'a'];
22+
}
23+
return count;
24+
}
25+
};

0 commit comments

Comments
 (0)