- 395. Longest Substring with At L
- Longest Substring with At Least
- Longest Substring with At Least
- Longest Substring with At Least
- LIS 和 LCS 子序列子串问题
- LeetCode 3. Longest Substring Wi
- Leetcode--Longest Palindromic Su
- Longest Substring Without Repeat
- 3. Longest Substring Without Rep
- 3. Longest Substring Without Rep
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.
网友评论