Check if a string can be converted to another string by replacing vowels and consonants in Python

Suppose we have two strings s and t. We can only change a character at any position to any vowel if it is already a vowel or to a consonant if it is already a consonant. We have to check whether s can be represented to t or vice-versa.

So, if the input is like s = "udpmva", t = "itmmve", then the output will be True as we can transform u -> i, d -> t, p -> m, a -> e

To solve this, we will follow these steps −

  • s_size := size of s
  • if s_size is not same as size of t , then
    • return False
  • for i in range 0 to s_size, do
    • if s[i] and t[i] are vowels, then
      • go for next iteration
    • otherwise when s[i] and t[i] are not vowels, then
      • go for next iteration
    • otherwise,
      • return False
  • return True

Let us see the following implementation to get better understanding −


 Live Demo

def isVowel(x):
   if x in ['a', 'e', 'i', 'o', 'u']:
      return True
   return False
def solve(s, t):
   s_size = len(s)
   if (s_size != len(t)):
      return False
   for i in range(s_size):
      if (isVowel(s[i]) and isVowel(t[i])):
      elif ((isVowel(s[i])) == False and ( isVowel(t[i]) == False)):
         return False
   return True
s, t = "udpgma", "itmmve"
print(solve(s, t))


"udpgma", "itmmve"



Updated on: 29-Dec-2020


Kickstart Your Career

Get certified by completing the course

Get Started