classSolution { public: string removeDuplicates(string s, int k){ string ans; vector<int> accu; // accumulate for (char c : s) { int new_accu; if (!ans.empty() && c == ans.back()) { new_accu = accu.back() + 1; } else { new_accu = 1; } if (new_accu >= k) { for (int i = 0; i < new_accu - 1; ++i) { ans.pop_back(); accu.pop_back(); } } else { ans.push_back(c); accu.push_back(new_accu); } } return ans; } };
1210. Minimum Moves to Reach Target with Rotations
It asks for the minimum number of steps, and n is at most 100, so BFS can solve it. The idea of this problem is not hard. The hard part is implementation. Since it is not a direct BFS, there are many details to pay attention to during implementation.
Time complexity: O(N ^ 2), space complexity: O(N ^ 2).
Because I recently tried learning the emerging language Rust, some of the following problems will use Rust instead of C++. The purpose is to give myself a chance to practice and become familiar with this new language. Students who have time and interest can also learn this new language. If your previous main language was C++, learning RUST is really satisfying.