CHEFSTR22 - Editorial

PROBLEM LINK:

Practice
Contest

Author: Aman Nautiyal
Tester: Aman Nautiyal
Editorialist: Suyash Saxena

DIFFICULTY:

SIMPLE

PREREQUISITES:

None

PROBLEM:

Check if a given string s can be changed to another given string t, given that the only mutations possible in s are that vowels can be replaced by other vowels and consonants can be replaced by other consonants.

QUICK EXPLANATION:

If the strings s and c are of same length and have vowels and consonants at the same indices, then they are mutable, otherwise not.

EXPLANATION:

Since we cannot add or remove any characters from the string s to mutate it to string t, hence if the lengths of the strings s and t are unequal, we can simply print no. No matter what characters we replace in the string s it will never be equal to t as the number of characters are different.

We only need to check further if the length of both the strings is same. In that case we need to check if both the strings have vowels and consonants at the same places. For example: “abc” and “efg” have vowels at first index and consonants on the remaining ones. Therefore we can change a to e (vowel to vowel), b to f (consonant to consonant) and c to g (consonant to consonant) and make the string “abc” equal to “efg”. But if the vowels and consonants in one string are at different positions than the other one, then it will be impossible to change the first string to be equal to the second since we can only replace vowels with vowels and consonants with consonants. For example if string s is “abc” and string t is “def”, then we can’t do anything to change string s to be equal to t. ‘a’ can only be replaced by a vowel, but in the string t, the first letter is a consonant, i.e., ‘d’. Hence it is impossible to change string s to string t.

This gives us the logic that we need to implement:

  1. if length of s \neq length of t, print no.
  2. else
  3. \quad set a flag variable to true
  4. \quad loop through the strings
  5. \qquad if s[i] is a vowel and t[i] is a vowel or if s[i] is a consonant and t[i] is a consonant then continue.
  6. \qquad else set flag to false, print no and exit the loop.
  7. \quad if flag is set to true at this point, print yes
  8. end

SOLUTIONS:

Editorialist's Solution
#include <iostream>
using namespace std;
// helper function to check if a character is vowel or nor
bool isVowel(char ch) {
    if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        return true;
    return false;
}

int main() {
	// your code goes here
    string s, t;
    cin>>s>>t;
    if (s.length() != t.length()) {
        cout<<"no\n";
        return 0;
    }
    for(int i=0; i<s.length(); i++) {
        if((isVowel(s[i]) && isVowel(t[i])) || (!isVowel(s[i]) && !isVowel(t[i])))
            continue;
        else {
            cout<<"no\n";
            return 0;
        }
    }
    cout<<"yes\n";
return 0;
}