From f0643a0f10bc6ca0af744c6b6e57c575f1b6b0eb Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Fri, 8 May 2026 20:00:09 +0100 Subject: [PATCH] sol --- Medium/String_Compression/Solution.cpp | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Medium/String_Compression/Solution.cpp diff --git a/Medium/String_Compression/Solution.cpp b/Medium/String_Compression/Solution.cpp new file mode 100644 index 0000000..d2c689b --- /dev/null +++ b/Medium/String_Compression/Solution.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + int compress(vector& chars) { + int count = 0; + + vector compressed = {}; + char temp = chars.at(0); + + for (int i = 0; i < chars.size(); i++) + { + char current = chars.at(i); + + if (temp == current) // If temp and current == same then increase count + { + count++; + } + else + { + compressed.push_back(temp); + if (1 < count) { + string s = to_string(count); + for (char c : s) { + compressed.push_back(c); + } + } + temp = current; + count = 1; + } + } + + compressed.push_back(temp); + if (1 < count) { + string s = to_string(count); + for (char c : s) { + compressed.push_back(c); + } + } + chars = compressed; + + return (compressed.size()); // Get the size of the letters found + the amount found and return it + } +}; \ No newline at end of file