给定三元字符串中至少包含一次字符的子字符串计数
给定大小为N的字符串str仅由0 、 1和2组成,任务是找到至少一次由字符0 、 1和2组成的子字符串的数量。
例子:
Input: str = “0122”
Output: 2
Explanation:
There exists 2 substrings such that the substrings has characters 0, 1, 2 at least once is “012” and “0122”. Therefore, the count of substrings is 2.
Input: S = “00021”
Output: 3
方法:给定的问题可以使用滑动窗口技术来解决,其想法是制作大小为 3的频率数组,其中包含0 、 1和2的出现。遍历给定的字符串并相应地更新freq数组,如果数组中的所有 3 个索引都大于零,则计算它们中的最小值并将其递增到变量count中。请按照以下步骤解决问题:
- 初始化一个大小为3的数组freq[]以存储数组中所有元素的频率。
- 将变量count初始化为0以存储答案,将i初始化为0以维护左指针。
- 使用变量j迭代范围[0, N)并执行以下任务:
- 将数组freq[]中当前字符str[ I ]的频率增加1。
- 在while循环中遍历直到freq[0]、freq[1]和freq[2]大于0,将第i个位置的字符的频率减1 ,将i的值加1。
- 将i的值添加到变量计数中。
- 执行上述步骤后,打印count的值作为答案。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
#include
using namespace std;
// Function to count the number of
// substrings consists of 0, 1, and 2
int countSubstrings(string& str)
{
// Initialize frequency array
// of size 3
int freq[3] = { 0 };
// Stores the resultant count
int count = 0;
int i = 0;
// Traversing string str
for (int j = 0; j < str.length(); j++) {
// Update frequency array
freq[str[j] - '0']++;
// If all the characters are
// present counting number of
// substrings possible
while (freq[0] > 0 && freq[1] > 0 && freq[2] > 0) {
freq[str[i++] - '0']--;
}
// Update number of substrings
count += i;
}
// Return the number of substrings
return count;
}
// Driver Code
int main()
{
string str = "00021";
int count = countSubstrings(str);
cout << count;
return 0;
}
// This code is contributed by Kdheeraj.
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to count the number of
// substrings consists of 0, 1, and 2
public static int countSubstrings(String str)
{
// Initialize frequency array
// of size 3
int[] freq = new int[3];
// Stores the resultant count
int count = 0;
int i = 0;
// Traversing string str
for (int j = 0;
j < str.length(); j++) {
// Update frequency array
freq[str.charAt(j) - '0']++;
// If all the characters are
// present counting number of
// substrings possible
while (freq[0] > 0 && freq[1] > 0
&& freq[2] > 0) {
freq[str.charAt(i++) - '0']--;
}
// Update number of substrings
count += i;
}
// Return the number of substrings
return count;
}
// Driver Code
public static void main(String[] args)
{
String str = "00021";
System.out.println(
countSubstrings(str));
}
}
Python3
# Python program for above approach
# Function to count the number of
# substrings consists of 0, 1, and 2
def countSubstrings(str):
# Initialize frequency array
# of size 3
freq = [ 0 ]*3
# Stores the resultant count
count = 0
i = 0
# Traversing string str
for j in range ( 0 ,len(str)):
# Update frequency array
freq[ord(str[j]) - ord('0')] += 1
# If all the characters are
# present counting number of
# substrings possible
while (freq[0] > 0 and freq[1] > 0 and freq[2] > 0):
i += 1
freq[ord(str[i]) - ord('0')] -= 1
# Update number of substrings
count += i
# Return the number of substrings
return count
# Driver Code
str = "00021"
count = countSubstrings(str)
print(count)
# This code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG {
// Function to count the number of
// substrings consists of 0, 1, and 2
public static int countSubstrings(string str)
{
// Initialize frequency array
// of size 3
int[] freq = new int[3];
// Stores the resultant count
int count = 0;
int i = 0;
// Traversing string str
for (int j = 0;
j < str.Length; j++) {
// Update frequency array
freq[str[j] - '0']++;
// If all the characters are
// present counting number of
// substrings possible
while (freq[0] > 0 && freq[1] > 0
&& freq[2] > 0) {
freq[str[i++] - '0']--;
}
// Update number of substrings
count += i;
}
// Return the number of substrings
return count;
}
// Driver Code
public static void Main(String[] args)
{
string str = "00021";
Console.Write(countSubstrings(str));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)