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
Program to check typed string is for writing target string in stuck keyboard keys or not in Python
Sometimes keyboard keys get stuck and produce repeated characters when typing. This program checks if a typed string could have been intended to write a target string, accounting for stuck keys that repeat characters.
For example, if we want to type "apple" but the 'p' and 'e' keys are stuck, we might end up typing "appppleee". This program verifies if such typing errors are possible.
Problem Statement
Given two strings s (typed string) and t (target string), determine if s could be the result of typing t with some stuck keyboard keys.
Example
If s = "appppleee" and t = "apple", the output should be True because:
- 'a' matches 'a' ?
- 'ppp' could be from stuck 'p' key ?
- 'l' matches 'l' ?
- 'eee' could be from stuck 'e' key ?
Algorithm
The solution uses a two-pointer approach to traverse both strings:
- Use pointers
ifor typed stringsandjfor target stringt - Track the last matched character to identify stuck keys
- For each character in
s, check if it matches the current target character or is a repeat of the last matched character - Handle remaining characters in
safter processing all oft
Implementation
def solve(s, t):
i = j = 0
s_len = len(s)
t_len = len(t)
t_last = ""
while j < t_len:
if i == s_len:
return False
if s[i] == t[j]:
t_last = t[j]
i += 1
j += 1
elif s[i] == t_last:
i += 1
else:
return False
if i < s_len:
return all(char == t_last for char in s[i:])
else:
return True
# Test the function
s = "appppleee"
t = "apple"
print(solve(s, t))
True
How It Works
The algorithm works by:
-
Character Matching: When
s[i] == t[j], we advance both pointers and updatet_last -
Stuck Key Detection: When
s[i] == t_last, we advance only the typed string pointer (handling repeated characters) - Invalid Character: If neither condition is met, the typing is impossible
- Remaining Characters: After processing target string, any remaining characters in typed string must all be repeats of the last matched character
Additional Examples
# Test various cases
test_cases = [
("hello", "hello", True), # Perfect match
("hellooo", "hello", True), # Stuck 'o' key
("helllo", "hello", True), # Stuck 'l' key
("helo", "hello", False), # Missing character
("helloworld", "hello", False) # Extra different character
]
for s, t, expected in test_cases:
result = solve(s, t)
print(f"s='{s}', t='{t}' ? {result} (Expected: {expected})")
s='hello', t='hello' ? True (Expected: True) s='hellooo', t='hello' ? True (Expected: True) s='helllo', t='hello' ? True (Expected: True) s='helo', t='hello' ? False (Expected: False) s='helloworld', t='hello' ? False (Expected: False)
Conclusion
This algorithm efficiently checks if a typed string could result from stuck keyboard keys using a two-pointer approach. It handles both character matching and stuck key detection in O(n+m) time complexity where n and m are the lengths of the strings.
---