10/14 每日一題(檢查是重複輸入就通過, 雙指針)
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.
You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
Example 1:
Input: name = "alex", typed = "aaleex" Output: true Explanation: 'a' and 'e' in 'alex' were long pressed.
Example 2:
Input: name = "saeed", typed = "ssaaedd" Output: false Explanation: 'e' must have been pressed twice, but it was not in the typed output.
Constraints:
1 <= name.length, typed.length <= 1000nameandtypedconsist of only lowercase English letters.
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
'檢查相對位置, 雙指針'
l,r=0,0
n1,n2=len(name),len(typed)
while r < n2:
if l < n1 and name[l] == typed[r]:
l+=1
r+=1
elif r>0 and typed[r]==typed[r-1]:
r+=1
else:
#出現不重複字元#
# print(f'l={l},r={r}')
return False
return l==n1
標籤: leetcode

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁