微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

Example 1:

Input:
s = "aaabb",k = 3

Output:
3

The longest substring is "aaa",as ‘a‘ is repeated 3 times. 

Example 2:

Input:
s = "ababbc",k = 2

Output:
5

The longest substring is "ababb",as ‘a‘ is repeated 2 times and ‘b‘ is repeated 3 times.

找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 的长度。

示例 1:

输入:
s = "aaabb",k = 3

输出:
3

最长子串为 "aaa" ,其中 ‘a‘ 重复了 3 次。

示例 2:

输入:
s = "ababbc",k = 2

输出:
5

最长子串为 "ababb" ,其中 ‘a‘ 重复了 2 次, ‘b‘ 重复了 3 次。
16ms
 1 class@H_502_46@ Solution {
 2     func longestSubstring(_ s: String,_ k: Int) ->@H_502_46@ Int {
 3         guard s.count > 0 else@H_502_46@ {
 4             return 0
 5 @H_502_46@        } 
 6         
 7         guard k > 0 else@H_502_46@ {
 8             return@H_502_46@ s.count
 9 @H_502_46@        }
10         
11         var result =@H_502_46@ Int.min
12         var sArray = s.map{ String($0@H_502_46@) }
13         
14         func splitString(string@H_502_46@: [String]) {
15             guard string.count > 0 else@H_502_46@ {
16                 return
17 @H_502_46@            }
18             var memo =@H_502_46@ [String: Int]()
19             var invalidChar =@H_502_46@ [String]()
20             for i in 0..<string@H_502[email protected] {
21                 memo[string[i]] = memo[string[i],default:0] + 1
22 @H_502_46@            }
23 
24             var candidates =@H_502_46@ [[String]]()
25             var last = -1
26             var i = 0
27             while i < string@H_502[email protected]  {
28                 if memo[string[i]]! <@H_502_46@ k {
29                     if i <= last + 1@H_502_46@ {
30                         last =@H_502_46@ i
31                         i += 1
32                         continue
33                     } else@H_502_46@ {
34                         candidates.append(Array(string[last + 1..<@H_502_46@i]))
35                         last =@H_502_46@ i
36 @H_502_46@                    }
37 @H_502_46@                }
38                 i += 1
39 @H_502_46@            }
40             
41             if last < string.count - 1@H_502_46@{
42                 candidates.append(Array(string[last + 1..<string@H_502[email protected]]))
43 @H_502_46@            }
44             
45             if last == -1@H_502_46@ {
46                 result = max(string@H_502[email protected],result)
47                 return
48 @H_502_46@            }
49             
50             for c in@H_502_46@ candidates {
51                 splitString(string@H_502_46@:c)
52 @H_502_46@            }
53 @H_502_46@        }
54         
55         splitString(string@H_502_46@:sArray)
56         return result == Int.min ? 0@H_502_46@ : result
57 @H_502_46@    }
58 }

20ms

 1 class@H_502_46@ Solution {
 2     func longestSubstring(_ s: String,_ k: Int) ->@H_502_46@ Int {
 3         return helper(Array(s),0@H_502_46@,s.count,k)
 4 @H_502_46@    }
 5     
 6     func helper(_ s: [Character],_ left: Int,_ right: Int,_ k: Int) ->@H_502_46@ Int {
 7         guard right - left >= k else { return 0@H_502_46@ }
 8         var counts =@H_502_46@ [Character: Int]()
 9         for char in s[left..<@H_502_46@right] {
10             counts[char] = (counts[char] ?? 0) + 1
11 @H_502_46@        }
12         
13         for i in left..<right where counts[s[i]]! <@H_502_46@ k {
14             var j = i + 1
15             while j < right && counts[s[j]]! < k { j += 1@H_502_46@ }
16             return@H_502_46@ max(helper(s,left,i,k),helper(s,j,right,k))
17 @H_502_46@        }
18         return right -@H_502_46@ left
19 @H_502_46@    }
20 }

52ms

 1 class@H_502_46@ Solution {
 2     func longestSubstring(_ s: String,_ k: Int) ->@H_502_46@ Int {
 3         var arr =@H_502_46@ Array(s)
 4         var cmap:[Character:Int] =@H_502_46@ [:]
 5         
 6         if s.length == 0@H_502_46@ {
 7             return 0
 8 @H_502_46@        }
 9         
10         for c in@H_502_46@ arr {
11             if cmap[c] ==@H_502_46@ nil {
12                 cmap[c] = 0
13 @H_502_46@            }
14             cmap[c] = cmap[c]! + 1
15 @H_502_46@        }
16         
17         var leastfreq = arr.count+1
18         var leastfreqchar:Character? =@H_502_46@ nil
19         for (letter,count) in@H_502_46@ cmap {
20             if count <@H_502_46@ leastfreq {
21                 leastfreq =@H_502_46@ count
22                 leastfreqchar =@H_502_46@ letter
23 @H_502_46@            }
24 @H_502_46@        }
25         
26         if leastfreq >=@H_502_46@ k {
27             return@H_502_46@ s.count
28 @H_502_46@        }        
29         
30         var begin = 0
31         var maxsofar = 0
32         for i in 0@H_502[email protected] {
33             if i == arr.count || arr[i] ==@H_502_46@ leastfreqchar {
34                 let tmp = Array(arr[begin..<@H_502_46@i])
35                 let tmps:String =@H_502_46@ String(tmp)
36                 maxsofar =@H_502_46@ max(maxsofar,longestSubstring(tmps,k))
37                 begin = i+1
38 @H_502_46@            }
39 @H_502_46@        }
40         
41         return@H_502_46@ maxsofar
42 @H_502_46@    }
43 }

56ms

 1 class@H_502_46@ Solution {
 2     func longestSubstring(_ s: String,_ k: Int) ->@H_502_46@ Int {
 3         if k <= 1 { return@H_502_46@ s.count }
 4         
 5         let array =@H_502_46@ [Character](s)
 6         var dict =@H_502_46@ [Character: Int]()
 7         for c in@H_502_46@ array {
 8             if let count =@H_502_46@ dict[c] {
 9                 dict[c] = count + 1
10             } else@H_502_46@ {
11                 dict[c] = 1
12 @H_502_46@            }
13 @H_502_46@        }
14         
15         var result = 0
16         var separator: Character? =@H_502_46@ nil
17         for key in@H_502_46@ dict.keys {
18             if let count = dict[key],count <@H_502_46@ k {
19                 separator =@H_502_46@ key
20                 break
21 @H_502_46@            }
22 @H_502_46@        }
23         if let separator =@H_502_46@ separator {
24             let subStringArray =@H_502_46@ s.components(separatedBy: String(separator))
25             for subString in@H_502_46@ subStringArray {
26                 let temp =@H_502_46@ longestSubstring(subString,k)
27                 if temp >@H_502_46@ result {
28                     result =@H_502_46@ temp
29 @H_502_46@                }
30 @H_502_46@            }
31         } else@H_502_46@ {
32             return@H_502_46@ s.count
33 @H_502_46@        }
34         return@H_502_46@ result
35 @H_502_46@    }
36 }

764ms

 1 class@H_502_46@ Solution {
 2     func longestSubstring(_ s: String,_ k: Int) ->@H_502_46@ Int {
 3       if s.count <@H_502_46@ k {
 4         return 0
 5 @H_502_46@    }
 6     var dict =@H_502_46@ [Character: Int]()
 7     var i = 0
 8     var j = 0
 9     let ss =@H_502_46@ Array(s)
10     var maxLength = 0
11     for k in@H_502_46@ s {
12         dict[k] = (dict[k] ?? 0) + 1
13 @H_502_46@    }
14     let dict2 = dict.filter{$0.value <@H_502_46@ k}
15     if dict2.count == 0@H_502_46@ {
16         return@H_502_46@ s.count
17 @H_502_46@    }
18     while j <@H_502_46@ s.count {
19         if let d =@H_502_46@ dict2[ss[j]] {
20             if d <@H_502_46@ k {
21                 let start =@H_502_46@ s.index(s.startIndex,offsetBy: i)
22                 let end =@H_502_46@ s.index(s.startIndex,offsetBy: j)
23                 let range = start..<@H_502_46@end
24                 let subS =@H_502_46@ s[range]
25                 maxLength =@H_502_46@ max(maxLength,longestSubstring(String(subS),k))
26                 i = j + 1
27 @H_502_46@            }
28 @H_502_46@        }
29         j += 1
30 @H_502_46@    }
31     let start =@H_502_46@ s.index(s.startIndex,offsetBy: i)
32     let end =@H_502_46@ s.index(s.startIndex,offsetBy: j)
33     let range = start..<@H_502_46@end
34     let subS =@H_502_46@ s[range]
35     maxLength =@H_502_46@ max(maxLength,k))
36     return@H_502_46@ maxLength
37 @H_502_46@    }
38 }

948ms

 1 class@H_502_46@ Solution {
 2     func longestSubstring(_ s: String,_ k: Int) ->@H_502_46@ Int {
 3         var n:Int =@H_502_46@ s.count
 4         var max_idx:Int = 0
 5         var res:Int = 0     
 6         var m:[Int] = [Int](repeating:0,count: 128@H_502_46@)
 7         var ok:Bool = true
 8         for c in@H_502_46@ s.characters
 9 @H_502_46@        {
10             m[c.ascii] += 1
11 @H_502_46@        }
12         for i in 0..<@H_502_46@n
13 @H_502_46@        {
14             if m[s[i].ascii] <@H_502_46@ k
15 @H_502_46@            {
16                 res = max(res,longestSubstring(s.subString(max_idx,i -@H_502_46@ max_idx),k))
17                 ok = false
18                 max_idx = i + 1
19 @H_502_46@            }
20 @H_502_46@        }
21         return ok ? n : max(res,n -@H_502_46@ max_idx),k)) 
22 @H_502_46@    }
23 @H_502_46@}
24 
25 @H_502_46@extension String {        
26     //subscript函数可以检索数组中的值
27     //直接按照索引方式截取指定索引的字符
28     subscript (_ i: Int) ->@H_502_46@ Character {
29         //读取字符
30         get {return@H_502_46@ self[index(startIndex,offsetBy: i)]}
31 @H_502_46@    }
32     
33     // 截取字符串:指定索引和字符数
34     // - begin: 开始截取处索引
35     // - count: 截取的字符数量
36     func subString(_ begin:Int,_ count:Int) ->@H_502_46@ String {
37         let start = self.index(self.startIndex,offsetBy: max(0@H_502_46@,begin))
38         let end = self.index(self.startIndex,offsetBy:  min(self.count,begin +@H_502_46@ count))
39         return String(self[start..<@H_502_46@end]) 
40 @H_502_46@    }
41 @H_502_46@}
42 
43 //Character扩展方法  
44 @H_502_46@extension Character  
45 @H_502_46@{  
46   //属性:ASCII整数值(定义小写为整数值)
47    var@H_502_46@ ascii: Int {
48         get@H_502_46@ {
49             let s =@H_502_46@ String(self).unicodeScalars
50             return@H_502_46@ Int(s[s.startIndex].value)
51 @H_502_46@        }
52 @H_502_46@    }
53 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐