Two Pointer Method
Two Pointer Method
Leetcode 925
- Long Pressed Name
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.
Examples
Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.
Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
Input: name = "leelee", typed = "lleeelee"
Output: true
Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.
Constraints:
1 <= name.length <= 1000
1 <= typed.length <= 1000
name and typed contain only lowercase English letters.
Solutions
Here are some cases for when we are allowed to skip characters of typed
.
- First we run a loop to move the two pointers along the strings, until we reach the end of either string .
- For each character in
name
, if there is a match with the next character intyped
, we` advance both pointers. - If they are mismatch , and it is the first character of the block in
typed
, the answer is False. - Else , discard all similar characters of
typed
coming up . The next different character coming must match .
- At the end of the loop, we would end up three cases:
- If there is still some characters left unmatched in the
name
string , then we do not have a match - If there is still some characters left in the
typed
string, and all the remaining characters are resulted from the long press, then we still have a match . - Otherwise , if any of the remaining characters in the
typed
string is not redundant , then we do not have a match .
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
np, tp = 0, 0
while np < len(name) and tp < len(typed):
if name[np] == typed[tp]:
np += 1
tp += 1
elif tp >= 1 and typed[tp] == typed[tp-1]:
tp += 1
else:
return False
if np != len(name):
return False
else:
while tp < len(typed):
if typed[tp] != typed[tp-1]:
return False
tp += 1
return True
advance two pointers , until we exhaust one of the strings.
while np < len(name) and tp < len(typed):
if name[np] == typed[tp]:
np += 1
tp += 1
elif tp >= 1 and typed[tp] == typed[tp-1]:
tp += 1
else:
return False
-
If there is still some characters left unmatched in the origin string, then we don’t have a match. eg. name = “abc” typed = “aabb”
-
In the case that there are some redundant characters left in typed
we could still have a match. eg. name = “abc” typed = “abccccc”
if np != len(name):
return False
else:
while tp < len(typed):
if typed[tp] != typed[tp-1]:
return False
tp += 1
# both strings have been consumed
return True
Leetcode Solution Section
相關文章
- rust-quiz:018-method-or-function-pointer.rsRustUIFunction
- AP(Access Pointer)
- C++ pointerC++
- Tagged Pointer 字串字串
- pointer-events屬性
- Numerical Results of RhDYas CG method and RhLHas CG method
- coca after two months vs in two months
- Kernel Method
- Swizzling Method
- Greedy Method
- golang unsafe.Pointer與uintptrGolangUI
- CSS3 pointer-eventsCSSS3
- LeetCode 138. Copy List with Random PointerLeetCoderandom
- div新增cursor:pointer;失效問題。
- Two Pirates - 2
- IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokensExceptionHTTP
- [vue] computed 和 methodVue
- Go語言之methodGo
- what is the Mixin method in Python?Python
- CSS pointer-events屬性的使用CSS
- 11.23 Two Different Worlds
- LeetCode | 1 Two SumLeetCode
- Tokitsukaze and Two Colorful Tapes
- F - Two Sequence Queries
- Merge Two Sorted List
- Leetcode 231 Power of TwoLeetCode
- Leetcode 1 two sumLeetCode
- methodHandle* method, JavaCallArguments* args, TRAPSJava
- The House of Mind (FASTBIN METHOD) PRIMEAST
- 工廠方法(Factory Method)
- method.invoke(...)反射點反射
- form&method【POST~GET】ORM
- WPF 開啟Pointer訊息存在的坑
- rust-quiz:011-function-pointer-comparison.rsRustUIFunction
- ISO C++ forbids comparison between pointer and integer [-fpermissive]C++ORB
- The Network Program Log Two (Scapy)
- LeetCode | 349 Intersection Of Two ArraysLeetCode
- H-Two Convex PolygonsGo