PROBLEM LINK:
Author: Naman Mittal
Tester: Aditya Rana
Editorialist: Deepanshu Jindal
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Strings, Basic Programming
PROBLEM:
For a given string S, comprising of only lowercase English alphabets, eliminate the vowels from the string that occur between two consonants.
QUICK EXPLANATION:
Iterate over the string by checking adjacent characters. If a vowel is sandwiched between consonants, then don’t add it in the resultant string.
EXPLANATION:
Iterate over the string S. For any valid index i, if the character present at index i is a vowel, and characters present at index i-1 and i+1 are both consonants, then we will not consider vowel present at index i in our resultant string. Otherwise, we can unhesitatingly add them to our resultant string.
Complexities
Time Complexity: O(N) per test case, where N is the length of string.
Auxillary Space Complexity: O(N) per test case, where N is the length of string.
SOLUTIONS:
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
unordered_set<char> us({'a', 'e', 'i', 'o', 'u'});
bool isVowel(char c) {
return us.find(c) != us.end();
}
string solve(string &s) {
string ans = "";
for(int i = 0; i < s.size(); ++i) {
if(i == 0 || i == s.size() - 1) {
ans += s[i];
continue;
}
if(isVowel(s[i]) && !isVowel(s[i - 1]) && !isVowel(s[i + 1])) continue;
ans += s[i];
}
return ans;
}
int main() {
int t; cin >> t;
while(t--) {
string s; cin >> s;
cout << solve(s) << endl;
}
return 0;
}