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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • 清水镇是铁路旁的一个小镇。镇上有两个火车站,一个属于地方,另一个属于科研机构。这也是这个镇子之所以存在并且曾经一度...
    韩有财阅读 286评论 0 1
  • 在我看来自救就是自律。 高中老师说上了大学就轻松了,其实不是的,学习是一辈子的事情,而不是阶段性的学习。 我们这一...
    生生花阅读 244评论 0 0