1. 程式人生 > >[Swift]LeetCode424. 替換後的最長重復字符 | Longest Repeating Character Replacement

[Swift]LeetCode424. 替換後的最長重復字符 | Longest Repeating Character Replacement

ons 中間 win scalar sts 截取 set case output

Given a string that consists of only uppercase English letters, you can replace any letter in the string with another letter at most k times. Find the length of a longest substring containing all repeating letters you can get after performing the above operations.

Note:
Both the string‘s length and k

will not exceed 104.

Example 1:

Input:
s = "ABAB", k = 2

Output:
4

Explanation:
Replace the two ‘A‘s with two ‘B‘s or vice versa. 

Example 2:

Input:
s = "AABABBA", k = 1

Output:
4

Explanation:
Replace the one ‘A‘ in the middle with ‘B‘ and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.

給你一個僅由大寫英文字母組成的字符串,你可以將任意位置上的字符替換成另外的字符,總共可最多替換 k 次。在執行上述操作後,找到包含重復字母的最長子串的長度。

註意:
字符串長度 和 k 不會超過 104。

示例 1:

輸入:
s = "ABAB", k = 2

輸出:
4

解釋:
用兩個‘A‘替換為兩個‘B‘,反之亦然。

示例 2:

輸入:
s = "AABABBA", k = 1

輸出:
4

解釋:
將中間的一個‘A‘替換為‘B‘,字符串變為 "AABBBBA"。
子串 "BBBB" 有最長重復字母, 答案為 4。

88ms
 1
class Solution { 2 func characterReplacement(_ s: String, _ k: Int) -> Int { 3 var count: [Character: Int] = [:] 4 let s = Array(s) 5 let n = s.count 6 var start = 0 7 var end = 0 8 var res = 0 9 var maxCount = 0 10 while end < n { 11 count[s[end]] = (count[s[end]] ?? 0) + 1 12 maxCount = max(maxCount, count[s[end]]!) 13 while end - start + 1 - maxCount > k { 14 count[s[start]] = count[s[start]]! - 1 15 start += 1 16 } 17 res = max(res, end - start + 1) 18 end += 1 19 } 20 return res 21 } 22 }

280ms

 1 class Solution {
 2     func characterReplacement(_ s: String, _ k: Int) -> Int {
 3       var slidingWindows = Array(repeating: 0, count: 26)
 4         var endIndex = 0
 5         var startIndex = 0
 6         var maxRepeatCount = 0
 7         var maxCount = 0
 8         let chats = s.utf8CString
 9         let count = chats.count - 1
10         while endIndex < count{
11             let currentIndex = Int(chats[endIndex] - 65)
12             slidingWindows[currentIndex] += 1
13             maxCount = max(maxCount, slidingWindows[currentIndex])
14             if endIndex - startIndex + 1 - maxCount > k {
15                 slidingWindows[Int(chats[startIndex] - 65)] -= 1
16                 startIndex += 1
17                 slidingWindows.forEach { (element) in
18                     if element > maxCount {
19                         maxCount = element
20                     }
21                 }
22             }
23             maxRepeatCount = max(maxRepeatCount, endIndex - startIndex + 1)
24             endIndex += 1
25         }
26         return maxRepeatCount
27     }
28 }

6792ms

 1 class Solution {
 2     func characterReplacement(_ s: String, _ k: Int) -> Int {
 3         var res:Int = 0
 4         var maxCnt:Int = 0
 5         var start:Int = 0
 6         var counts:[Int] = [Int](repeating:0,count:26)
 7         //A:65
 8         for i in 0..<s.count
 9         {
10             var num:Int = s[i].ascii - 65
11             counts[num] += 1
12             maxCnt = max(maxCnt,counts[num])
13             while(i - start + 1 - maxCnt > k)
14             {
15                 counts[s[start].ascii - 65] -= 1
16                 start += 1
17             }
18             res = max(res, i - start + 1)            
19         }
20         return res
21     }
22 }
23 
24 extension String {        
25     //subscript函數可以檢索數組中的值
26     //直接按照索引方式截取指定索引的字符
27     subscript (_ i: Int) -> Character {
28         //讀取字符
29         get {return self[index(startIndex, offsetBy: i)]}
30         
31     }
32 }
33 
34 extension Character  
35 {  
36   //屬性:ASCII整數值(定義小寫為整數值)
37    var ascii: Int {
38         get {
39             let s = String(self).unicodeScalars
40             return Int(s[s.startIndex].value)
41         }
42     }
43 }

[Swift]LeetCode424. 替換後的最長重復字符 | Longest Repeating Character Replacement