Related Articles
Check if a string can be converted to another string by replacing vowels and consonants
• Difficulty Level : Hard
• Last Updated : 18 Feb, 2019

Given two strings S1 and S2, you are allowed only to change a character at any position to any vowel if it is a vowel or to a consonant, if it is a consonant. The task is to check if a string S1 can be changed to S2 or S2 can be changed to S1.

Examples:

Input: S1 = “abcgle”, S2 = “ezggli”
Output: Yes
Change ‘a’ to ‘e’, ‘b’ to ‘z’, ‘c’ to ‘g’ and ‘e’ to ‘i’.

Input: S1 = “abc”, S2 = “cgth”
Output: No

Approach: The following conditions should be followed to solve the above problem:

• The length of both the string should be equal.
• At every index, the character of S1 and S2 should be both vowel or consonant.

Below is the implementation of the above approach:

## C++

 `// C++ program to check if a string can be converted ` `// to other string by replacing vowels and consonants ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if the character ` `// is vowel or not ` `bool` `isVowel(``char` `c) ` `{ ` `    ``if` `(c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'` `|| c == ``'u'``) ` `        ``return` `true``; ` ` `  `    ``return` `false``; ` `} ` ` `  `// Function that checks if a string can be ` `// converted to another string ` `bool` `checkPossibility(string s1, string s2) ` `{ ` `    ``// Find length of string ` `    ``int` `l1 = s1.length(); ` `    ``int` `l2 = s2.length(); ` ` `  `    ``// If length is not same ` `    ``if` `(l1 != l2) ` `        ``return` `false``; ` ` `  `    ``// Iterate for every character ` `    ``for` `(``int` `i = 0; i < l1; i++) { ` `        ``// If both vowel ` `        ``if` `(isVowel(s1[i]) && isVowel(s2[i])) ` `            ``continue``; ` ` `  `        ``// Both are consonants ` `        ``else` `if` `(!(isVowel(s1[i])) && !(isVowel(s2[i]))) ` `            ``continue``; ` `        ``else` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``string S1 = ``"abcgle"``, S2 = ``"ezggli"``; ` `    ``if` `(checkPossibility(S1, S2)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to check if a string  ` `// can be converted to other string ` `// by replacing vowels and consonants  ` `class` `GfG ` `{  ` ` `  `// Function to check if the character  ` `// is vowel or not  ` `static` `boolean` `isVowel(``char` `c)  ` `{  ` `    ``if` `(c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| ` `                    ``c == ``'o'` `|| c == ``'u'``)  ` `        ``return` `true``;  ` ` `  `    ``return` `false``;  ` `}  ` ` `  `// Function that checks if a string can be  ` `// converted to another string  ` `static` `boolean` `checkPossibility(String s1, String s2)  ` `{  ` `    ``// Find length of string  ` `    ``int` `l1 = s1.length();  ` `    ``int` `l2 = s2.length();  ` ` `  `    ``// If length is not same  ` `    ``if` `(l1 != l2)  ` `        ``return` `false``;  ` ` `  `    ``// Iterate for every character  ` `    ``for` `(``int` `i = ``0``; i < l1; i++)  ` `    ``{  ` `        ``// If both vowel  ` `        ``if` `(isVowel(s1.charAt(i)) && ` `            ``isVowel(s2.charAt(i)))  ` `            ``continue``;  ` ` `  `        ``// Both are consonants  ` `        ``else` `if` `(!(isVowel(s1.charAt(i))) &&  ` `                    ``!(isVowel(s2.charAt(i))))  ` `            ``continue``;  ` `        ``else` `            ``return` `false``;  ` `    ``}  ` `    ``return` `true``;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String[] args)  ` `{  ` ` `  `    ``String S1 = ``"abcgle"``; ` `    ``String S2 = ``"ezggli"``;  ` `    ``if` `(checkPossibility(S1, S2) == ``true``)  ` `        ``System.out.println(``"Yes"``);  ` `    ``else` `        ``System.out.println(``"No"``);  ` `} ` `}  ` ` `  `// This code is contributed by Prerna saini. `

## Python3

 `# Python3 program to check if a string can  ` `# be converted to other string by replacing ` `# vowels and consonants ` ` `  `# Function to check if the character ` `# is vowel or not ` `def` `isVowel(c): ` ` `  `    ``if` `(c ``=``=` `'a'` `or` `c ``=``=` `'e'` `or` `        ``c ``=``=` `'i'` `or` `c ``=``=` `'o'` `or` `c ``=``=` `'u'``): ` `        ``return` `True` ` `  `    ``return` `False` ` `  `# Function that checks if a string can  ` `# be converted to another string ` `def` `checkPossibility(s1, s2): ` ` `  `    ``# Find length of string ` `    ``l1 ``=` `len``(s1) ` `    ``l2 ``=` `len``(s2) ` ` `  `    ``# If length is not same ` `    ``if` `(l1 !``=` `l2): ` `        ``return` `False` ` `  `    ``# Iterate for every character ` `    ``for` `i ``in` `range``(l1):  ` `         `  `        ``# If both vowel ` `        ``if` `(isVowel(s1[i]) ``and` `isVowel(s2[i])): ` `            ``continue` ` `  `        ``# Both are consonants ` `        ``elif` `((isVowel(s1[i])) ``=``=` `False` `and`  `              ``(isVowel(s2[i]) ``=``=` `False``)): ` `            ``continue` `        ``else``: ` `            ``return` `False` `     `  `    ``return` `True` ` `  `# Driver Code ` `S1, S2 ``=` `"abcgle"``, ``"ezggli"` `if` `(checkPossibility(S1, S2)): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# program to check if a string  ` `// can be converted to other string  ` `// by replacing vowels and consonants  ` `using` `System; ` ` `  `class` `GfG  ` `{  ` ` `  `    ``// Function to check if the character  ` `    ``// is vowel or not  ` `    ``static` `bool` `isVowel(``char` `c)  ` `    ``{  ` `        ``if` `(c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `||  ` `                        ``c == ``'o'` `|| c == ``'u'``)  ` `            ``return` `true``;  ` `     `  `        ``return` `false``;  ` `    ``}  ` `     `  `    ``// Function that checks if a string can be  ` `    ``// converted to another string  ` `    ``static` `bool` `checkPossibility(``string` `s1, ``string` `s2)  ` `    ``{  ` `        ``// Find length of string  ` `        ``int` `l1 = s1.Length ;  ` `        ``int` `l2 = s2.Length ;  ` `     `  `        ``// If length is not same  ` `        ``if` `(l1 != l2)  ` `            ``return` `false``;  ` `     `  `        ``// Iterate for every character  ` `        ``for` `(``int` `i = 0; i < l1; i++)  ` `        ``{  ` `            ``// If both vowel  ` `            ``if` `(isVowel(s1[i]) &&  ` `                ``isVowel(s2[i]))  ` `                ``continue``;  ` `     `  `            ``// Both are consonants  ` `            ``else` `if` `(!(isVowel(s1[i])) &&  ` `                        ``!(isVowel(s2[i])))  ` `                ``continue``;  ` `            ``else` `                ``return` `false``;  ` `        ``}  ` `        ``return` `true``;  ` `    ``}  ` ` `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `     `  `        ``string` `S1 = ``"abcgle"``;  ` `        ``string` `S2 = ``"ezggli"``;  ` `        ``if` `(checkPossibility(S1, S2) == ``true``)  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``else` `            ``Console.WriteLine(``"No"``);  ` `    ``} ` `}  ` ` `  `// This code is contributed by Ryuga.  `

Output:

```Yes
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :