[Swift]LeetCode925. 長按鍵入 | Long Pressed Name
阿新 • • 發佈:2018-10-21
note 多次 字母 ont The num his set etc
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 wasn‘t in the typed output.
Example 3:
Input: name = "leelee", typed = "lleeelee"
Output: true
Example 4:
Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It‘s not necessary to long press any character.
Note:
name.length <= 1000
typed.length <= 1000
- The characters of
name
andtyped
are lowercase letters.
你的朋友正在使用鍵盤輸入他的名字 name
。偶爾,在鍵入字符 c
時,按鍵可能會被長按,而字符可能被輸入 1 次或多次。
你將會檢查鍵盤輸入的字符 typed
。如果它對應的可能是你的朋友的名字(其中一些字符可能被長按),那麽就返回 True
。
示例 1:
輸入:name = "alex", typed = "aaleex" 輸出:true 解釋:‘alex‘ 中的 ‘a‘ 和 ‘e‘ 被長按。
示例 2:
輸入:name = "saeed", typed = "ssaaedd" 輸出:false 解釋:‘e‘ 一定需要被鍵入兩次,但在 typed 的輸出中不是這樣。
示例 3:
輸入:name = "leelee", typed = "lleeelee" 輸出:true
示例 4:
輸入:name = "laiden", typed = "laiden" 輸出:true 解釋:長按名字中的字符並不是必要的。
提示:
name.length <= 1000
typed.length <= 1000
name
和typed
的字符都是小寫字母。
36ms
1 class Solution { 2 func isLongPressedName(_ name: String, _ typed: String) -> Bool { 3 var p:Int = 0 4 for num in 0..<typed.count 5 { 6 var typeIndex = typed.index(typed.startIndex,offsetBy: num) 7 var nameIndex = name.index(name.startIndex,offsetBy: p) 8 if p < name.count && typed[typeIndex] == name[nameIndex] 9 { 10 p += 1 11 } 12 else 13 { 14 if num > 0 && typed[typeIndex] == typed[typed.index(typed.startIndex,offsetBy: num - 1)] 15 { 16 continue 17 } 18 else 19 { 20 return false 21 } 22 } 23 } 24 return p == name.count 25 } 26 }
[Swift]LeetCode925. 長按鍵入 | Long Pressed Name