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.git
Example 1:github
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 。輸出 T 的長度。app
示例 1:less
輸入: s = "aaabb", k = 3 輸出: 3 最長子串爲 "aaa" ,其中 'a' 重複了 3 次。
示例 2:ui
輸入: s = "ababbc", k = 2 輸出: 5 最長子串爲 "ababb" ,其中 'a' 重複了 2 次, 'b' 重複了 3 次。 來源:力扣(LeetCode) 連接:https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters 著做權歸領釦網絡全部。商業轉載請聯繫官方受權,非商業轉載請註明出處。
# -*- coding: utf-8 -*- # @Author: 何睿 # @Create Date: 2019-08-27 21:07:09 # @Last Modified by: 何睿 # @Last Modified time: 2019-08-28 08:40:37 from collections import Counter class Solution: def longestSubstring(self, s: str, k: int) -> int: for key, v in Counter(s).items(): if v < k: return max(self.longestSubstring(sub, k) for sub in s.split(key)) return len(s)