包含 0、1 和 2 的最小窗口
给定一个大小为N的由字符0、1 和 2 组成的字符串S ,任务是找到包含所有三个字符0、1 和 2 的字符串S的最小子串的长度。如果不存在这样的子串,则返回-1。
例子:
Input: S = “01212”
Output: 3
Explanation: The substring 012 is the smallest substring
that contains the characters 0, 1 and 2.
Input: S = “12121”
Output: -1
Explanation: As the character 0 is not present in the
string S, therefor no substring containing
all the three characters 0, 1 and 2
exists. Hence, the answer is -1 in this case.
方法:该方法的思想如下所述:
Use three pointers to store the indices of the elements 0, 1 and 2. When all the three elements are found, the distance between the maximum of them and the minimum of them is the minimum size window.
Keep updating the pointers whenever any of them is found again and calculate the size of the new window.
请按照下图更好地理解。
插图:
Consider S = “01212” and th three pointers to be zeroindex, oneindex and twoindex and all of them are -1 initially.
When i = 0:
=> S[i] = ‘0’. zeroindex = 0, oneindex = -1, twoindex = -1
=> All of the values are not found. So no window is possible
When i = 1:
=> S[i] = ‘1’. zeroindex = 0, oneindex = 1, twoindex = -1
=> All of the values are not found. So no window is possible
When i = 2:
=> S[i] = ‘2’. zeroindex = 0, oneindex = 1, twoindex = 2
=> All of the values are found.
=> Maximum is twoindex = 2. Minimum is zeroindex = 0.
=> So window size = (2 – 0 + 1) = 3.
=> Minimum window size = 3
When i = 3:
=> S[i] = ‘1’. zeroindex = 0, oneindex = 3, twoindex = 2
=> All of the values are found.
=> Maximum is oneindex = 3. Minimum is zeroindex = 0.
=> So window size = (3 – 0 + 1) = 4.
=> Minimum window size = min (3, 4) = 3
When i = 4:
=> S[i] = ‘2’. zeroindex = 0, oneindex = 3, twoindex = 4
=> All of the values are found.
=> Maximum is twoindex = 4. Minimum is zeroindex = 0.
=> So window size = (4 – 0 + 1) = 5.
=> Minimum window size = min(3, 5) = 3
So the size of the smallest window is 3
请按照以下步骤解决问题:
- 取三个变量zero 、 one和two来检查窗口中是否找到了 0、1 和 2。
- 取三个变量zeroindex 、 oneindex和twoindex ,当我们遇到它们时,它们将存储 0、1 和 2 的索引。
- 对 String 的整个长度运行 for 循环:
- 更新遇到的值的索引。
- 如果找到其中三个,则更新窗口的长度。
- 长度将是索引 0、1 和 2 的最大值和最小值之间的差。
- 如果遍历结束后没有找到所有三个值,即 0、1、2,则在这种情况下返回“-1”。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the length of
// the smallest substring
int smallestSubstring(string S)
{
int res = INT_MAX;
// To check 0, 1 and 2
bool zero = false, one = false, two = false;
// To store indexes of 0, 1 and 2
int zeroindex, oneindex, twoindex, j = 0;
for (int i = 0; i < S.length(); i++) {
if (S[i] == '0') {
zero = true;
zeroindex = j;
}
else if (S[i] == '1') {
one = true;
oneindex = j;
}
else if (S[i] == '2') {
two = true;
twoindex = j;
}
// Calculating length
if (zero and one and two)
res = min(res,
max({ zeroindex,
oneindex,
twoindex })
- min({ zeroindex,
oneindex,
twoindex }));
j++;
}
// Egde case
if (res == INT_MAX)
return -1;
return res + 1;
}
// Driver Code
int main()
{
string S = "01212";
// Function call
cout << smallestSubstring(S);
return 0;
}
Javascript
3
时间复杂度: O(N)
辅助空间: O(1)