mirror of
https://github.com/mudabbir-ahmad/Leet-Code-prog.git
synced 2026-10-07 18:30:20 +00:00
sol
This commit is contained in:
1 parent
b29cd3775d
commit
f0643a0f10
1 file changed
+42
@@ -0,0 +1,42 @@
|
||||
class Solution {
|
||||
public:
|
||||
int compress(vector<char>& chars) {
|
||||
int count = 0;
|
||||
|
||||
vector<char> 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
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user