PROBLEM LINK:
Setter: Rishav
Tester: Nitin Pundir
Editorialist: Sourav Singh
DIFFICULTY
Easy
PREREQUISITES
Observations, Strings’ operations
PROBLEM
Given a string S, you have to perform the following manipulations
- Remove all vowels.
- Convert all lowercase alphabets to uppercase.
- Place ‘@’ before every consonant.
APPROACH 1
-
First we can convert string to UPPER CASE (Manually or using in-built operations)
-
For each character in string S, we can check whether the character is consonant or not.
How will we do that?
One way is to store all consonants in a string and for each character in the given string, we will check whether that character exists in the “consonants string” or not.
TIME COMPLEXITY
The time complexity is O(N) . Because of the loop from 1 to N.
SOLUTIONS
Setter's Solution
string s;
cin >> s;
string res = "";
transform(s.begin(), s.end(), s.begin(), ::toupper);
for(auto x : s) {
if(x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U') {
continue;
}
if(x >= ‘0’ && x <= ‘9’) {
continue;
}
res += ‘@’;
res += x;
}
cout << res << “\n”;
Tester's Solution
#include<iostream>
#include <vector>
#include <cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<math.h>
#include <list>
#include<bitset>
#define ll long long
using namespace std;
ll md=1000000007;
ll diff=-1;
string mins;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
t=1;
while(t--)
{
string s;
cin >> s;
string cons="bcdfghjklmnpqrstvwxyz";
transform(s.begin(), s.end(), s.begin(), ::toupper);
transform(cons.begin(), cons.end(), cons.begin(), ::toupper);
for(auto x : s) {
for(auto y: cons)
{
if(x==y)
{
cout<<"@"<<x;
}
}
}
}
return 0;
}
Feel free to share your approach. Suggestions are welcomed as always.