在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a string Example 1: Input: S = 5
Output: 6
Explanation:
There are 6 substrings they are : 'havef','avefu','vefun','efuno','etcod','tcode'.
Example 2: Input: S = 5
Output: 0
Explanation:
Notice K can be larger than the length of S. In this case is not possible to find any substring.
Note:
给你一个字符串 示例 1: 输入:S = "havefunonleetcode", K = 5 输出:6 解释: 这里有 6 个满足题意的子串,分别是:'havef','avefu','vefun','efuno','etcod','tcode'。 示例 2: 输入:S = "home", K = 5 输出:0 解释: 注意:K 可能会大于 S 的长度。在这种情况下,就无法找到任何长度为 K 的子串。 提示:
44ms 1 class Solution { 2 func numKLenSubstrNoRepeats(_ S: String, _ K: Int) -> Int { 3 var ans:Int = 0 4 let n:Int = S.count 5 let S:[Int] = Array(S).map{$0.ascii} 6 for i in 0..<n 7 { 8 var freq:[Int] = [Int](repeating:0,count:26) 9 var j:Int = i 10 var len:Int = 0 11 while(j < n) 12 { 13 //a:97 14 if freq[S[j] - 97] != 0 {break} 15 freq[S[j] - 97] += 1 16 len += 1 17 j += 1 18 if len == K 19 { 20 ans += 1 21 } 22 } 23 } 24 return ans 25 } 26 } 27 28 //Character扩展 29 extension Character 30 { 31 //Character转ASCII整数值(定义小写为整数值) 32 var ascii: Int { 33 get { 34 return Int(self.unicodeScalars.first?.value ?? 0) 35 } 36 } 37 }
|
请发表评论