← Home

1347. Minimum Number of Steps to Make Two Strings Anagram

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
134

This is one of those problems where the clean idea matters more than the amount of code. For 1347. Minimum Number of Steps to Make Two Strings Anagram, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minSteps, count.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 168 ms, faster than 16.18% of C++ online submissions for Minimum Number of Steps to Make Two Strings Anagram.
02//Memory Usage: 18.3 MB, less than 100.00% of C++ online submissions for Minimum Number of Steps to Make Two Strings Anagram.
03
04class Solution {
05public:
06    int minSteps(string s, string t) {
07        map<char, int> scount, tcount;
08        int ans = 0;
09        
10        for(char c : s){
11            scount[c]++;
12        }
13        
14        for(char c : t){
15            tcount[c]++;
16        }
17        
18        for(auto it = tcount.begin(); it != tcount.end(); it++){
19            //focus on the count of char needed to changed to
20            if(scount.find(it->first) == scount.end()){
21                ans += it->second;
22            }else{
23                ans += max(it->second - scount[it->first], 0);
24            }
25        }
26        
27        return ans;
28    }
29};
30
31//Sum the difference
32//https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/discuss/503450/JavaPython-3-Count-occurrences-and-sum-the-difference-w-analysis.
33//Runtime: 84 ms, faster than 93.62% of C++ online submissions for Minimum Number of Steps to Make Two Strings Anagram.
34//Memory Usage: 18 MB, less than 100.00% of C++ online submissions for Minimum Number of Steps to Make Two Strings Anagram.
35class Solution {
36public:
37    int minSteps(string s, string t) {
38        vector<int> count(26);
39        
40        for(int i = 0; i < s.size(); i++){
41            count[t[i]-'a']++;
42            count[s[i]-'a']--;
43        }
44        
45        int (*iabs)(int) = &std::abs;
46        transform(count.begin(), count.end(), count.begin(), iabs);
47        
48        return accumulate(count.begin(), count.end(), 0)/2;
49    }
50};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.