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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
return true ;
return false ;
}
bool checkPossibility(string s1, string s2)
{
int l1 = s1.length();
int l2 = s2.length();
if (l1 != l2)
return false ;
for ( int i = 0; i < l1; i++) {
if (isVowel(s1[i]) && isVowel(s2[i]))
continue ;
else if (!(isVowel(s1[i])) && !(isVowel(s2[i])))
continue ;
else
return false ;
}
return true ;
}
int main()
{
string S1 = "abcgle" , S2 = "ezggli" ;
if (checkPossibility(S1, S2))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GfG
{
static boolean isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' )
return true ;
return false ;
}
static boolean checkPossibility(String s1, String s2)
{
int l1 = s1.length();
int l2 = s2.length();
if (l1 != l2)
return false ;
for ( int i = 0 ; i < l1; i++)
{
if (isVowel(s1.charAt(i)) &&
isVowel(s2.charAt(i)))
continue ;
else if (!(isVowel(s1.charAt(i))) &&
!(isVowel(s2.charAt(i))))
continue ;
else
return false ;
}
return true ;
}
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" );
}
}
|
Python3
def isVowel(c):
if (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or c = = 'u' ):
return True
return False
def checkPossibility(s1, s2):
l1 = len (s1)
l2 = len (s2)
if (l1 ! = l2):
return False
for i in range (l1):
if (isVowel(s1[i]) and isVowel(s2[i])):
continue
elif ((isVowel(s1[i])) = = False and
(isVowel(s2[i]) = = False )):
continue
else :
return False
return True
S1, S2 = "abcgle" , "ezggli"
if (checkPossibility(S1, S2)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GfG
{
static bool isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' )
return true ;
return false ;
}
static bool checkPossibility( string s1, string s2)
{
int l1 = s1.Length ;
int l2 = s2.Length ;
if (l1 != l2)
return false ;
for ( int i = 0; i < l1; i++)
{
if (isVowel(s1[i]) &&
isVowel(s2[i]))
continue ;
else if (!(isVowel(s1[i])) &&
!(isVowel(s2[i])))
continue ;
else
return false ;
}
return true ;
}
public static void Main()
{
string S1 = "abcgle" ;
string S2 = "ezggli" ;
if (checkPossibility(S1, S2) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isVowel(c) {
if (c === "a" || c === "e" || c === "i" || c === "o" || c === "u" )
return true ;
return false ;
}
function checkPossibility(s1, s2)
{
var l1 = s1.length;
var l2 = s2.length;
if (l1 !== l2) return false ;
for ( var i = 0; i < l1; i++)
{
if (isVowel(s1[i]) && isVowel(s2[i])) continue ;
else if (!isVowel(s1[i]) && !isVowel(s2[i])) continue ;
else return false ;
}
return true ;
}
var S1 = "abcgle" ,
S2 = "ezggli" ;
if (checkPossibility(S1, S2)) document.write( "Yes" );
else document.write( "No" );
</script>
|
Time Complexity: O(n), as we are using a loop to traverse n times, where n is the size of a given string s1.
Auxiliary Space: O(n), as we are passing the whole string as a value to the method.