Skip to content

Commit

Permalink
Solve: String Compression
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Oct 12, 2024
1 parent 6b96c78 commit bbcd8ba
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LeetCode/String Compression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int compress(vector<char>& chars) {
int n = chars.size();
int write = 0, read = 0;

while (read < n) {
char currentChar = chars[read];
int count = 0;

while (read < n && chars[read] == currentChar) {
read++;
count++;
}

chars[write] = currentChar;
write++;

if (count > 1) {
for (char c : to_string(count)) {
chars[write] = c;
write++;
}
}
}

return write;
}
};

0 comments on commit bbcd8ba

Please sign in to comment.