Check if a string is the typed name of the given name in Python

PythonServer Side ProgrammingProgramming

Suppose we have two lowercase strings s and t. Sometimes, when we type a vowel, the key might get long pressed, and the vowel will be repeated 1 or more times. We have to check whether it is possible that t is typed that indicates s or not.

So, if the input is like s = "mine" t = "miiine", then the output will be True as vowel 'i' is repeated three times, other letters are fine.

To solve this, we will follow these steps −

  • s_len := size of s
  • t_len := size of t
  • j := 0
  • for i in range 0 to s_len - 1, do
    • if s[i] is not same as t[j], then
      • return False
    • if s[i] is not vowel, then
      • j := j + 1
      • go for next iteration
    • cnt_1 := 1
    • while i < s_len - 1 and s[i] is same as s[i + 1], do
      • cnt_1 := cnt_1 + 1
      • i := i + 1
    • cnt_2 := 1
    • while j < t_len - 1 and t[j] is same as s[i], do
      • cnt_2 := cnt_2 + 1
      • j := j + 1
    • if cnt_1 > cnt_2, then
      • return False
  • return True

Let us see the following implementation to get better understanding −

Example

 Live Demo

def isVowel(c):
   vowel = "aeiou"
   return c in vowel
def solve(s, t):
   s_len = len(s)
   t_len = len(t)
   j = 0
   for i in range(s_len):
      if s[i] != t[j]:
         return False
      if isVowel(s[i]) == False:
         j = j + 1
         continue
      cnt_1 = 1
      while i < s_len - 1 and (s[i] == s[i + 1]):
         cnt_1 = cnt_1 + 1
         i = i + 1
      cnt_2 = 1
      while j < t_len - 1 and t[j] == s[i]:
         cnt_2 = cnt_2 + 1
         j = j + 1
      if cnt_1 > cnt_2:
         return False
   return True
s = "mine"
t = "miiine"
print(solve(s, t))

Input

"mine", "miiine"

Output

True
raja
Published on 29-Dec-2020 17:57:45
Advertisements