String with dollar

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

Chef has given a string and and his mother told him to show all the letter of string with the help of “$” sign

if the letters are repeated in string then he has to use a same number of “$” sign

  • Note: all the letters are lowercase letter in the given string .

for example : jklj

o/p= here j is represent like j:$$ after it we have to print comma sign then next k is represent single time then j: then print 1 comma and after it l is represent as l: because there is single 1 in string that’s why 1 dollar is used

we have to print the output is first character from string then colon then dollar sign (dollar sign = number of time character is present in string) after it comma sign then continue it

EXPLANATION:

In above question we have given string we have to count the character and print it with appropriate number of dollar sign exceeded with colon then print a comma

for example given string

abaa

output = a:$$$,b:$

In above example a is presented in string is 3 times that’s why it is represented in output is 3 time and b is represent 1 time that’s why it is shown with 1 dollar sign

we have to first count the character then print appropriate number of dollar sign exceeded with colon along with character and after it print comma sign

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a[130]={0},cnt=0;
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
int k=s[i];
a[k]++;
if(a[k]==1)
{
cnt++;
}
}
for(int i=0;i<s.length();i++)
{
int t=s[i];
int p=a[t];
if(a[t]!=0){
cout<<s[i]<<":";
for(int j=0;j<p;j++)
{
cout<<"$";
}
if(cnt>=2){
cout<<","; cnt–;
}
a[t]=0;

 }

}
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a[130]={0},cnt=0;
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
int k=s[i];
a[k]++;
if(a[k]==1)
{
cnt++;
}
}
for(int i=0;i<s.length();i++)
{
int t=s[i];
int p=a[t];
if(a[t]!=0){
cout<<s[i]<<":";
for(int j=0;j<p;j++)
{
cout<<"$";
}
if(cnt>=2){
cout<<","; cnt–;
}
a[t]=0;

 }

}
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a[130]={0},cnt=0;
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
int k=s[i];
a[k]++;
if(a[k]==1)
{
cnt++;
}
}
for(int i=0;i<s.length();i++)
{
int t=s[i];
int p=a[t];
if(a[t]!=0){
cout<<s[i]<<":";
for(int j=0;j<p;j++)
{
cout<<"$";
}
if(cnt>=2){
cout<<","; cnt–;
}
a[t]=0;

 }

}
cout<<endl;
}
return 0;
}