Longest Substring with At Least K Repeating Characters

a b a b b c d b a, k=2

First, we consider the situation that only one kind of char satisfies the condition, which is to say,

the same letter needs to occur at least k times to be effective, and in the above example, only the substring "bb" satisfies the condition, thus in this loop, we need to update the result to 2.

Here is what we do in this loop:

We use two pointers to specify the start and the end position of the effective substring. The end pointer marks the char that we're exploring.

The start pointer moves right when the end pointer points to the second kind of char, and it wil continue go right until the string between start and end contains only the char that's the same as the end char. and we calculate (end-start) and compare it with k, when (end-start) > k, we update the result with max(result, (end-start)).

e.x.,

a b a b b c d b a

s

e

a b a b b c d b a

​ s

​ e

end - start= 2, and we can update the result to 2, then we continue explore.

a b a b b c d b a

​ s

​ e

a b a b b c d b a

​ s

​ e

a b a b b c d b a

​ s

​ e

a b a b b c d b a

​ s

​ e

We finish the exploring of finding the longest substring containing only one kind of char and get the result of 2.

推荐阅读更多精彩内容

  • 在我看来自救就是自律。 高中老师说上了大学就轻松了,其实不是的,学习是一辈子的事情,而不是阶段性的学习。 我们这一...
    生生花阅读 100评论 0 0