★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ozyeonwx-kn.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git
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
.github
Example 1:微信
Input: text = "ababa" Output: 3
Example 2:app
Input: text = "aaabaaa" Output: 6
Example 3:spa
Input: text = "aaabbaaa" Output: 4
Example 4:code
Input: text = "aaaaa" Output: 5
Example 5:htm
Input: text = "abcdef" Output: 1
Constraints:blog
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 }