- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- if s[i] and t[i] are vowels, then
- return True
Let us see the following implementation to get better understanding −
Example
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])): continue elif ((isVowel(s[i])) == False and ( isVowel(t[i]) == False)): continue else: return False return True s, t = "udpgma", "itmmve" print(solve(s, t))
Input
"udpgma", "itmmve"
Output
True
Advertisements