From 5b13265374bafa81fad04d632f446e831f9fd207 Mon Sep 17 00:00:00 2001 From: jeetshah410 Date: Thu, 17 Jul 2025 08:55:00 +0530 Subject: [PATCH 1/2] Add Run Length Encoding and Decoding [#2719] --- compression/run_length_encoding.cpp | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 compression/run_length_encoding.cpp diff --git a/compression/run_length_encoding.cpp b/compression/run_length_encoding.cpp new file mode 100644 index 00000000000..57c66ed0a76 --- /dev/null +++ b/compression/run_length_encoding.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +// Run Length Encoding +string encode(const string& input) { + string encoded = ""; + int n = input.length(); + for (int i = 0; i < n; i++) { + int count = 1; + while (i < n - 1 && input[i] == input[i + 1]) { + count++; + i++; + } + encoded += input[i] + to_string(count); + } + return encoded; +} + +// Run Length Decoding +string decode(const string& input) { + string decoded = ""; + int n = input.length(); + for (int i = 0; i < n; i += 2) { + char ch = input[i]; + int count = input[i + 1] - '0'; + decoded += string(count, ch); + } + return decoded; +} + +// Driver code +int main() { + string original = "aaabbcdddd"; + string encoded = encode(original); + string decoded = decode(encoded); + + cout << "Original: " << original << endl; + cout << "Encoded: " << encoded << endl; + cout << "Decoded: " << decoded << endl; + + return 0; +} From 9eb06836f46019a32f0b7becc439389e909dc973 Mon Sep 17 00:00:00 2001 From: jeetshah410 Date: Thu, 17 Jul 2025 09:04:39 +0530 Subject: [PATCH 2/2] Move run_length_encoding.cpp to strings directory --- {compression => strings}/run_length_encoding.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {compression => strings}/run_length_encoding.cpp (100%) diff --git a/compression/run_length_encoding.cpp b/strings/run_length_encoding.cpp similarity index 100% rename from compression/run_length_encoding.cpp rename to strings/run_length_encoding.cpp