• 注册
• 查看作者
• # 领扣: 1638 最少划分子串

### 1638. Least Substring

Given a string containing n lowercase letters, the string needs to be divided into several continuous substrings, the letter in the substring should be same, and the number of letters in the substring does not exceed k, and output the minimal substring number meeting the requirement.

### Example

Example1

Input: s = "aabbbc", k = 2
Output: 4
Explanation:
we can get "aa", "bb", "b", "c" four substring.

Example2

input: s = "aabbbc", k = 3
Output: 3
we can get "aa", "bbb", "c" three substring.

### Notice

• n \leq 1e5

class Solution:
"""
@param s: the string s
@param k: the maximum length of substring
@return: return the least number of substring
"""
def getAns(self, s, k):
# Write your code here
length = len(s)
if length == 0 or k <0:
return 0

count = 1
total = 1

for i in range(1, length, 1):
if s[i] != s[i-1]:
total +=1
count =1
else:
if count < k:
count += 1
elif count == k:
count = 1
total += 1

return total

纽约州·纽约
• 0
• 0
• 0
• 390
• 请登录之后再进行评论

• 做任务
• 全部清空