[Swift]LeetCode925. 長按鍵入 | Long Pressed Name

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-aolxrbfd-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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.github

 Example 1:微信

Input: name = "alex", typed = "aaleex" Output: true Explanation: 'a' and 'e' in 'alex' were long pressed. 

Example 2:app

Input: name = "saeed", typed = "ssaaedd" Output: false Explanation: 'e' must have been pressed twice, but it wasn't in the typed output. 

Example 3:ide

Input: name = "leelee", typed = "lleeelee" Output: true 

Example 4:spa

Input: name = "laiden", typed = "laiden" Output: true Explanation: It's not necessary to long press any character. 

 Note:code

  1. name.length <= 1000
  2. typed.length <= 1000
  3. The characters of name and typed are lowercase letters.

你的朋友正在使用鍵盤輸入他的名字 name。偶爾,在鍵入字符 c 時,按鍵可能會被長按,而字符可能被輸入 1 次或屢次。htm

你將會檢查鍵盤輸入的字符 typed。若是它對應的多是你的朋友的名字(其中一些字符可能被長按),那麼就返回 Trueblog

示例 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
解釋:長按名字中的字符並非必要的。

 提示:

  1. name.length <= 1000
  2. typed.length <= 1000
  3. name 和 typed 的字符都是小寫字母。

8ms

 1 class Solution {
 2     func isLongPressedName(_ name: String, _ typed: String) -> Bool {
 3         guard name.count != 0 else {
 4             return true
 5         }
 6         let chars1 = Array(name)
 7         let chars2 = Array(typed)
 8         var index = 0
 9         for i in 0..<chars2.count {
10             if index == chars1.count {  // Best Idea
11                 if chars2[i] != chars2[i - 1] {
12                     return false
13                 }
14                 continue
15             }
16             
17             if chars1[index] == chars2[i] {
18                 index += 1
19             } else if i != 0 && chars2[i] == chars2[i - 1] {
20                 continue
21             } else {
22                 return false
23             }
24         }
25         return index == chars1.count
26     }
27 }

12ms

 1 class Solution {
 2   func count(_ str: String) -> [(Character, Int)] {
 3     var result = [(Character,Int)]()
 4     
 5     var pos = str.startIndex
 6     while pos < str.endIndex {
 7       var next = str.index(after:pos)
 8       var c = 1
 9       while next < str.endIndex && str[pos] == str[next] {
10         next = str.index(after:next)
11         c += 1
12       }
13       result.append((str[pos], c))
14       pos = next
15     }
16     
17     
18     return result
19   }
20   
21   
22   func isLongPressedName(_ name: String, _ typed: String) -> Bool {
23     let nameC = count(name)
24     let typedC = count(typed)
25     
26     if nameC.count != typedC.count {
27       return false
28     }
29     
30     for var i in 0..<nameC.count {
31       if nameC[i].0 != typedC[i].0 {
32         return false
33       }
34       if nameC[i].1 > typedC[i].1 {
35         return false
36       }
37     }
38     
39     return true
40   }
41 }

16ms

 1 class Solution {
 2     func isLongPressedName(_ name: String, _ typed: String) -> Bool {
 3         guard name.count != 0 else {
 4             return true
 5         }
 6         let chars1 = Array(name)
 7         let chars2 = Array(typed)
 8         var index = 0
 9         for i in 0..<chars2.count {
10             if index == chars1.count { // Best Idea
11                 if chars2[i] != chars2[i - 1] {
12                     return false
13                 }
14                 continue
15             }
16 
17             if chars1[index] == chars2[i] {
18                     index += 1
19             } else if i != 0 && chars2[i] == chars2[i - 1] {
20                 continue
21             } else {
22                 return false
23             }
24         }
25         return index == chars1.count
26     }
27 }

36ms

 1 class Solution {
 2     func isLongPressedName(_ name: String, _ typed: String) -> Bool {
 3         guard name.count != 0 else {
 4             return true
 5         }
 6         var index1 = 0
 7         var index2 = 0
 8         var char1 = Array(name)
 9         var char2 = Array(typed)
10         while index1 < name.count && index2 < typed.count {
11             if char1[index1] == char2[index2] {
12                 index1 += 1
13                 index2 += 1
14             } else if index2 != 0 && char2[index2] == char2[index2 - 1] {
15                     index2 += 1
16             } else {
17                 return false
18             }
19         }
20         return index1 == char1.count
21     }
22 }

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 }

44ms

 1 class Solution {
 2     func isLongPressedName(_ name: String, _ typed: String) -> Bool {
 3         guard name.count != 0 else {
 4             return true
 5         }
 6         
 7         var index1 = 0
 8         var index2 = 0
 9         var char1 = Array(name)
10         var char2 = Array(typed)
11         while index1 < name.count && index2 < typed.count {
12             if char1[index1] == char2[index2] {
13                 index1 += 1
14                 index2 += 1
15             } else {
16                 if index2 != 0 && char2[index2] == char2[index2 - 1] {
17                     index2 += 1
18                 } else {
19                     return false
20                 }
21             }
22         }
23         guard index1 == name.count else {
24             return false
25         }
26         
27         while index2 < typed.count {
28             if index2 != 0 && char2[index2] == char2[index2 - 1] {
29                 index2 += 1
30                 continue
31             } else {
32                 break
33             }
34         }
35         return index2 == typed.count
36     }
37 }

52ms

 1 class Solution {
 2     func isLongPressedName(_ name: String, _ typed: String) -> Bool {
 3         guard name.count != 0 else {
 4             return true
 5         }
 6         
 7         var index1 = 0
 8         var index2 = 0
 9         var char1 = Array(name)
10         var char2 = Array(typed)
11         while index1 < name.count && index2 < typed.count {
12             if char1[index1] == char2[index2] {
13                 index1 += 1
14                 index2 += 1
15             } else if index2 != 0 && char2[index2] == char2[index2 - 1] {
16                 index2 += 1
17             } else {
18                 return false
19             }
20         }
21         guard index1 == name.count else {
22             return false
23         }
24         
25         while index2 < typed.count {
26             if index2 != 0 && char2[index2] == char2[index2 - 1] {
27                 index2 += 1
28                 continue
29             } else {
30                 break
31             }
32         }
33         return index2 == typed.count
34     }
35 }
相關文章
相關標籤/搜索