aboutsummaryrefslogtreecommitdiff
path: root/other/string_compression.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/string_compression.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/string_compression.cpp')
-rw-r--r--other/string_compression.cpp31
1 files changed, 31 insertions, 0 deletions
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<char> &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<char>::iterator it, ty;
+ it = chars.begin() + ans.size();
+ ty = chars.begin() + chars.size();
+ chars.erase(it, ty);
+ return ans.size();
+ }
+};