In this HackerEarth Compress the String problem solution Given a string S of lowercase English alphabets of length N. You need to compress the string with the following rules:
  1. Convert the first letter to the uppercase
  2. Convert the substring consisting of all the consonants to the length of this substring (e.g. abcde to a3e)
  3. Remove the consecutive duplicate vowels (e.g. aaaeiia to aeia)


HackerEarth Compress the String problem solution



HackerEarth Compress the String problem solution.

#include <bits/stdc++.h>

using namespace std;

bool vowel(char c) {
    if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
        return 1;
    return 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while(t --) {
        int n;
        string s;
        cin >> n >> s;
        string ans = "";
        int i = 1;
        while(i < n) {
            if(vowel(s[i])) {
                while(i < n - 1 && s[i] == s[i + 1])
                    i ++;
                ans += s[i];
                i ++;
            } else {
                int cnt = 0;
                while(i < n && !vowel(s[i])) {
                    i ++;
                    cnt ++;
                }
                string str;
                stringstream strs;
                strs << cnt;
                strs >> str;
                ans += str;
            }
        }
        cout << char(s[0] - 32) << ans << '\n';
    }
    return 0;
}


Second solution

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "\n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
int main()
    {
        ios_base::sync_with_stdio(0);
        int t;
        cin >> t;
        assert(t >= 1 && t <= 1000);
        int len = 0;
        while(t--){
            int n;
            cin >> n;
            string s;
            cin >> s;
            len += s.length();
            assert(len >= 1 && len <= 1000000);
            s[0] = 'A' + (s[0] - 'a');
            cout << s[0];
            int idx = 1;
            int ctr = 0;
            while(idx < s.length()){
                if(s[idx] == 'a' || s[idx] == 'e' || s[idx] == 'i' || s[idx] == 'o' || s[idx] == 'u'){
                    if(ctr){
                        cout << ctr;
                        ctr = 0;
                    }
                    if(s[idx] != s[idx - 1]){
                        cout << s[idx];
                    }
                    ++idx;
                }
                else{
                    ++ctr;
                    ++idx;
                }
            }
            if(ctr){
                cout << ctr;
            }
            cout << endl;
        }   
    }