本文共 709 字,大约阅读时间需要 2 分钟。
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 = 3Output:3The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input:s = "ababbc", k = 2Output:5The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
找到字符串中的最长子字符串,这个子字符串所有字母出现的次数要大于k。
首先用set()来找到字符串中所有存在的字母,然后如果一个字母出现的次数小于k,则将字符串以这个字母(所有出现的地方)进行分割。
将分割后的字符串(此时已不包括上一次找到的出现次数小于k的字母了)重新进行上述操作。
直到分割后所有出现字符次数都大于k,返回此时字符串长度。
class Solution: def longestSubstring(self, s: str, k: int) -> int: setx = set(s) for cha in setx: if s.count(cha)
转载地址:http://jjrbb.baihongyu.com/