• [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(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.

    给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 次。在执行上述操作后,找到包含重复字母的最长子串的长度。

    注意:
    字符串长度 和 不会超过 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 }
  • 相关阅读:
    CentOS 5.5 系统安全配置
    从PHP5.2.x迁移到PHP5.3.x
    快要放假了!但为什么现在感到特别的累呢?!
    C#、Oracle、Sql server中拼音查询的函数
    C#实现Word中表格信息读取
    用 win2003 架设共享服务器[3]
    用 win2003 架设共享服务器[2]
    用 win2003 架设共享服务器[1]
    c#操作word表格
    Visual Studio 2008(VS2008)升级正式版
  • 原文地址:https://www.cnblogs.com/strengthen/p/10334069.html
Copyright © 2020-2023  润新知