Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

en/lcci/1.6/ #3920

giscus[bot] bot announced in Announcements
Jan 3, 2025 · 1 comment
Discussion options

en/lcci/1.6/

LeetCode solutions in any programming language

https://leetcode.doocs.org/en/lcci/1.6/

You must be logged in to vote

Replies: 1 comment

Comment options

// My approach is to do this question using single iteration ..
// My intuition is rau a loop from index 1 and check prev ele is same as current element if then increase the count.. and if prev element is not same diff we simply add the the prev alphabet and its count .to add count we need to convert the number into string using to_string function

#include <bits/stdc++.h>
using namespace std;

string compress(const string &s,int n ) {
int cnt = 1;
string comp;
for(int i = 1;i<n;i++){
if(s[i] == s[i-1]){
cnt++;
}else{
comp +=s[i-1];
comp += to_string(cnt);
cnt = 1;
}
}
comp += s[n-1];
comp += to_string(cnt);

return comp;

}
int main(){
string s = "aabbcccdb";
int n = s.size();
string ans = compress(s,n);
cout<<ans<<endl;
return 0;
}

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /