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

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 }

原文地址:https://www.cnblogs.com/strengthen/p/10334069.html

时间: 2024-10-14 00:56:49

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

leetcode424 替换后的最长重复字符 java题解

本题是比较典型的滑动窗口问题 这类问题一般通过一个滑动窗口就能在O(N)的时间复杂度下求解 本题可以先退化成考虑K=0的情况,此时题目就变成了求解字符串中最长连续子串长度问题了 我们先可以通过这个特例先了解一下滑动窗口的求解过程 上图的求解过程展示中,窗口从左至右不断扩张/滑动,当窗口触达字符串末尾字符时,运算结束,窗口的宽度为最终结果.初始窗口的宽度为1,我们不断的通过向当前窗口覆盖的子串后面追加一个字符看是否能满足我们的要求,如果满足窗口扩张,如果不满足,窗口向右滑动. 当K>0时,子串的条

424. 替换后的最长重复字符

题目: 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 注意:字符串长度 和 k 不会超过 104. 示例 1: 输入: s = "ABAB", k = 2 输出: 4 解释: 用两个'A'替换为两个'B',反之亦然. 示例 2: 输入: s = "AABABBA", k = 1 输出: 4 解释: 将中间的一个'A'替换为'B',字符串变为 "A

[LeetCode] Longest Repeating Character Replacement 最长重复字符置换

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 abo

查找字符串中最长重复字符的子串

temppos:记录子字符串开始的下标 list:存放重复的子字符串 public class RepeatString { private static void longestdupString(String s) { if (s == null || s.length() == 0) { return; } char temp = s.charAt(0); int temppos = 0; List<String> list = new ArrayList<String>()

[Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). E

[Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

No.3. 无重复字符的最长子串

3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.就是说,找一个最长的子串,这个子串没有重复字符 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew"

最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和

参考:http://www.ahathinking.com/archives/124.html 最长公共子序列 1.动态规划解决过程 1)描述一个最长公共子序列 如果序列比较短,可以采用蛮力法枚举出X的所有子序列,然后检查是否是Y的子序列,并记录所发现的最长子序列.如果序列比较长,这种方法需要指数级时间,不切实际. LCS的最优子结构定理:设X={x1,x2,……,xm}和Y={y1,y2,……,yn}为两个序列,并设Z={z1.z2.……,zk}为X和Y的任意一个LCS,则: (1)如果xm=

Leetcode3---&gt;无重复字符的最长子串长度

题目:给定一个字符串string,找出string中无重复字符的最长子串. 举例: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke"