[Swift]LeetCode1156. 单字符重复子串的最大长度 | Swap For Maximum Repeated Substring
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(www.zengqiang.org)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11333865.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
A string is a repeated character string if all characters in that string are the same. For example, "cccc"
is a repeated character string.
Return the largest L
such that: after either swapping two characters in text
or doing nothing, there exists a repeated character substring of text
with length L
.
Example 1:
Input: text = "ababa" Output: 3
Example 2:
Input: text = "aaabaaa" Output: 6
Example 3:
Input: text = "aaabbaaa" Output: 4
Example 4:
Input: text = "aaaaa" Output: 5
Example 5:
Input: text = "abcdef" Output: 1
Constraints:
1 <= text.length <= 20000
text
consist of lowercase English characters only.
如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。
给你一个字符串 text
,你只能交换其中两个字符一次或者什么都不做,然后得到一些单字符重复的子串。返回其中最长的子串的长度。
示例 1:
输入:text = "ababa" 输出:3
示例 2:
输入:text = "aaabaaa" 输出:6
示例 3:
输入:text = "aaabbaaa" 输出:4
示例 4:
输入:text = "aaaaa" 输出:5
示例 5:
输入:text = "abcdef" 输出:1
提示:
1 <= text.length <= 20000
text
仅由小写英文字母组成。
1 class Solution { 2 func maxRepOpt1(_ text: String) -> Int { 3 let chars = Array(text) 4 var idxdic = [Character: [Int]]() 5 for i in chars.indices { 6 let c = chars[i] 7 var arr = idxdic[c, default: [Int]()] 8 arr.append(i) 9 idxdic[c] = arr 10 } 11 var ans = 0 12 for (_, arr) in idxdic { 13 ans = max(ans, lenthOfArr(arr)) 14 } 15 return ans 16 } 17 18 func lenthOfArr(_ arr: [Int]) -> Int { 19 if arr.count == 0 { return 0 } 20 var tuples = [(Int, Int)]() 21 var curStar = arr[0] 22 var curEnd = arr[0] 23 for i in 1..<arr.count { 24 if arr[i]-curEnd == 1 { 25 curEnd = arr[i] 26 } else { 27 tuples.append((curStar, curEnd)) 28 curStar = arr[i] 29 curEnd = arr[i] 30 } 31 } 32 tuples.append((curStar, curEnd)) 33 var tuplefirst = tuples[0] 34 var ans = tuplefirst.1 - tuplefirst.0 + 1 35 for i in 1..<tuples.count { 36 let curTuple = tuples[i] 37 if curTuple.0 - tuplefirst.1 == 2 { 38 let lastLen = tuplefirst.1 - tuplefirst.0 + 1 39 let curLen = curTuple.1 - curTuple.0 + 1 40 ans = max(ans, lastLen+curLen) 41 if i != tuples.count - 1 || i > 1 { 42 ans = max(ans, lastLen+curLen+1) 43 } 44 } else { 45 let curLen = curTuple.1 - curTuple.0 + 1 46 let lastLen = tuplefirst.1 - tuplefirst.0 + 1 47 ans = max(ans, lastLen+1) 48 ans = max(ans, curLen+1) 49 } 50 tuplefirst = curTuple 51 } 52 return ans 53 } 54 }
120ms
1 class Solution { 2 func maxRepOpt1(_ text: String) -> Int { 3 let characters = Array(text) 4 var result = 0 5 var hash = [Character:[Int]]() 6 7 for (index, character) in characters.enumerated() { 8 hash[character] = (hash[character] ?? []) + [index] 9 } 10 11 for (key, value) in hash { 12 var consecutiveCount = 1, previousConsecutiveCount = 0, maximum = 0 13 var i = 1 14 15 while i < value.count { 16 if value[i] == value[i-1] + 1 { consecutiveCount += 1 } 17 else { 18 previousConsecutiveCount = value[i] == value[i-1] + 2 ? consecutiveCount : 0 19 consecutiveCount = 1 20 } 21 maximum = max(maximum, consecutiveCount + previousConsecutiveCount) 22 i += 1 23 } 24 25 maximum = max(maximum, consecutiveCount + previousConsecutiveCount) 26 result = max(result, maximum + (value.count > maximum ? 1 : 0)) 27 } 28 29 return result 30 } 31 }
Runtime: 144 ms
1 class Solution { 2 func maxRepOpt1(_ text: String) -> Int { 3 let arrT:[Character] = Array(text) 4 let n:Int = text.count 5 var best:Int = 0 6 let alphabet:[Character] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] 7 for c in alphabet 8 { 9 var count:Int = 0 10 for i in 0..<n 11 { 12 if arrT[i] == c 13 { 14 count += 1 15 } 16 } 17 if count == n 18 { 19 best = n 20 continue 21 } 22 for i in 0..<n 23 { 24 if arrT[i] != c 25 { 26 var left:Int = i 27 var right:Int = i 28 while (left > 0 && arrT[left - 1] == c) 29 { 30 left -= 1 31 } 32 while (right < n - 1 && arrT[right + 1] == c) 33 { 34 right += 1 35 } 36 var combined:Int = right - left 37 if combined == count 38 { 39 best = max(best, count) 40 } 41 else 42 { 43 best = max(best, combined + 1) 44 } 45 } 46 } 47 } 48 return best 49 } 50 }