Let's make this one less mysterious. For 443. String Compression, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are compress.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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
01//Runtime: 8 ms, faster than 86.14% of C++ online submissions for String Compression.
02//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for String Compression.
03
04class Solution {
05public:
06 int compress(vector<char>& chars) {
07 int pos = 0, count = 1;
08 char last = chars[pos];
09 pos++;
10
11 while(pos < chars.size()){
12 if(chars[pos] == last){
13 count++;
14 }
15 // cout << "pos: " << pos << ", count: " << count << endl;
16
17 //meet different char, or go to the end
18 if(chars[pos] != last || pos == chars.size()-1){
19 if(count > 1){
20 //remove redundant char
21 //the last char is not inclusive
22 chars.erase(chars.begin()+pos-count+1, chars.begin()+pos);
23 // cout << "last? " << (pos-count+1 == chars.size()-1) << endl;
24 // cout << "[" << pos-count+1 << ", " << pos << "] " << string(chars.begin(), chars.end());
25
26 //always insert at same position
27 //12: insert 2 and then insert 1 before 2
28 int insert_pos = pos-count+1;
29 /*
30 if we are at the end and we are dealing with this char(not previous),
31 then we need to insert at last
32 */
33 if((pos-count+1 == chars.size()-1) && chars[pos] == last)
34 insert_pos++;
35 // cout << ", insert_pos: " << insert_pos;
36
37 for(int count_tmp = count; count_tmp > 0; count_tmp /= 10){
38 /*
39 if not last, we are at the char different from last
40 if is last, we are at the char same as last
41 so need to add 1 for last
42 */
43 /*
44 after insert a number, move to next, so add i
45 */
46 chars.insert(chars.begin()+insert_pos, '0'+count_tmp%10);
47 //skip the inserted char
48 pos++;
49 }
50 // cout << ", " << string(chars.begin(), chars.end()) << endl;
51 //skip count-1 char
52 pos -= (count-1);
53 // pos -= (count-1);
54 }
55 count = 1;
56 last = chars[pos];
57 }
58 pos++;
59 }
60 return chars.size();
61 }
62};
63
64//Approach #1: Read and Write Heads
65//time: O(N), space: O(1)
66//Runtime: 8 ms, faster than 86.14% of C++ online submissions for String Compression.
67//Memory Usage: 8.1 MB, less than 100.00% of C++ online submissions for String Compression.
68class Solution {
69public:
70 int compress(vector<char>& chars) {
71 int N = chars.size();
72 int anchor = 0, write = 0;
73 for(int read = 0; read < N; read++){
74 if(read == N-1 || chars[read] != chars[read+1]){
75 //write char
76 chars[write++] = chars[anchor];
77 //write number
78 //more than one chars[anchor]
79 if(read > anchor){
80 int count = read - anchor + 1;
81 for(char c : to_string(count)){
82 chars[write++] = c;
83 }
84 }
85 //point to next char
86 anchor = read+1;
87 }
88 }
89 return write;
90 }
91};
Cost