★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-nbcqldpn-r.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
Note:
Both the string's length and k will not exceed 104.github
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。spa
示例 1:code
輸入: s = "ABAB", k = 2 輸出: 4 解釋: 用兩個'A'替換爲兩個'B',反之亦然。
示例 2:orm
輸入: 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 }
280mshtm
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 }