In my code, when I enter ‘a’ as input, output comes as ‘a111’ and for ‘ab’ input, output is coming as ‘a1b11’ - these are the two test cases that fail when I submit my code on Hackerblocks but I’m not able to find my mistake in the code. Please help.
Question: https://hack.codingblocks.com/contests/c/537/107
My code : https://ide.codingblocks.com/s/34591
String Compression
#include
#include
using namespace std;
string compress(string s,string out,int start,int count)
{
if(start>s.length())
{
return out;
}
if(s[start]==s[start-1])
{
return compress(s,out,start+1,count+1);
}
else
{
out+=to_string(count);
out+=(s[start]+’\0’);
return compress(s,out,start+1,1);
}
}
int main() {
string s,out;
cin>>s;
out=s[0]+’\0’;
cout<<compress(s,out,1,1);
return 0;
}
my output is correct but i am failing in all test cases. please help you
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
unordered_map<char, int> mp;
for(int i=0;i<s.length();i++){
mp[s[i]]++;
}
for(char i : s)
{
if(mp[i] != 0)
{
cout << i << mp[i];
mp[i] = 0;
}
}
return 0;
}
// can you suggest y this is not passing all test cases?