Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a string is the typed name of the given name in Python
Sometimes when we type on a keyboard, vowel keys might get long pressed, causing them to repeat one or more times. Given two lowercase strings s (the intended name) and t (the typed name), we need to check whether t could be a result of typing s with some vowels accidentally repeated.
So, if the input is like s = "mine" and t = "miiine", then the output will be True because the vowel 'i' is repeated three times while other letters remain unchanged.
Algorithm Steps
To solve this problem, we will follow these steps ?
- Get the length of both strings
sandt - Use two pointers to traverse both strings
- For each character in
s:- Check if it matches the current character in
t - If it's not a vowel, move to the next character in both strings
- If it's a vowel, count consecutive occurrences in both strings
- Ensure the typed string has at least as many repetitions as the original
- Check if it matches the current character in
- Return
Trueif all characters match properly
Implementation
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 j >= t_len or s[i] != t[j]:
return False
if not isVowel(s[i]):
j += 1
continue
# Count consecutive vowels in original string
cnt_1 = 1
while i < s_len - 1 and s[i] == s[i + 1]:
cnt_1 += 1
i += 1
# Count consecutive vowels in typed string
cnt_2 = 1
while j < t_len - 1 and t[j] == s[i]:
cnt_2 += 1
j += 1
# Typed string must have at least as many vowels as original
if cnt_1 > cnt_2:
return False
j += 1
# Check if we've consumed all characters in typed string
return j == t_len
# Test the function
s = "mine"
t = "miiine"
print(solve(s, t))
True
How It Works
The algorithm uses two pointers to compare characters:
- Non-vowels: Must match exactly between both strings
- Vowels: The typed string can have more repetitions than the original, but not fewer
- The function returns
Falseif characters don't match or if a vowel appears fewer times in the typed string
Example with Different Cases
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 j >= t_len or s[i] != t[j]:
return False
if not isVowel(s[i]):
j += 1
continue
cnt_1 = 1
while i < s_len - 1 and s[i] == s[i + 1]:
cnt_1 += 1
i += 1
cnt_2 = 1
while j < t_len - 1 and t[j] == s[i]:
cnt_2 += 1
j += 1
if cnt_1 > cnt_2:
return False
j += 1
return j == t_len
# Test cases
test_cases = [
("mine", "miiine"), # True - vowel repeated
("hello", "hellooo"), # True - vowel repeated
("cat", "ct"), # False - vowel missing
("love", "loove"), # True - vowel repeated
("world", "worrld") # False - consonant repeated
]
for s, t in test_cases:
result = solve(s, t)
print(f'"{s}" ? "{t}": {result}')
"mine" ? "miiine": True "hello" ? "hellooo": True "cat" ? "ct": False "love" ? "loove": True "world" ? "worrld": False
Conclusion
This algorithm efficiently checks if a typed string could result from long-pressing vowel keys while typing the original string. It ensures consonants match exactly while allowing vowels to be repeated in the typed version.
