From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/string_compression.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 other/string_compression.cpp (limited to 'other/string_compression.cpp') diff --git a/other/string_compression.cpp b/other/string_compression.cpp new file mode 100644 index 0000000..5f9b9c6 --- /dev/null +++ b/other/string_compression.cpp @@ -0,0 +1,31 @@ +#include "leetcode.h" + +class Solution { +public: + int compress(vector &chars) { + string ans; + int c = 1, i; + for (i = 1; i < chars.size(); i++) { + if (chars[i] == chars[i - 1]) + c++; + else if (chars[i] != chars[i - 1] && c == 1) + ans += chars[i - 1]; + else { + ans += chars[i - 1]; + ans += to_string(c); + c = 1; + } + } + if (c == 1) + ans += chars[i - 1]; + else + ans += chars[i - 1] + to_string(c); + for (int i = 0; i < ans.size(); i++) + chars[i] = ans[i]; + vector::iterator it, ty; + it = chars.begin() + ans.size(); + ty = chars.begin() + chars.size(); + chars.erase(it, ty); + return ans.size(); + } +}; -- cgit v1.2.3