[Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:为敢(WeiGanTechnologies)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10334069.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.
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了