diff --git a/LeetCode/String Compression.cpp b/LeetCode/String Compression.cpp new file mode 100644 index 0000000..70328a7 --- /dev/null +++ b/LeetCode/String Compression.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int compress(vector& 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; + } +}; \ No newline at end of file