给定一个字符串str和K个字符数组ARR [],任务是找到海峡的子串的数量只包含从给定的字符数组ARR字符[]。
注意:字符串str和arr []仅包含小写字母。
例子:
Input: S = “abcb”, K = 2, charArray[] = {‘a’, ‘b’}
Output: 4
Explanation:
The substrings are “a”, “ab”, “b”, “b” using the available characters
Input: S = “aabdbbtr”, K = 4, charArray[] = {‘e’, ‘a’, ‘r’, ‘t’}
Output: 6
Explanation:
The substrings “a”, “aa”, “a”, “t”, “tr”, “r” using the available characters.
天真的方法:天真的方法是为给定的字符串str生成所有可能的子字符串,并检查每个子字符串是否由数组arr []中的给定字符组成。如果是,则计算该子字符串,否则检查下一个子字符串。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:可以优化上述幼稚的方法,因为我们将删除在给定数组arr []中不存在的由字符形成的子字符串的数量。步骤如下:
- 将存在于数组arr []中的字符存储在大小为26的布尔数组中,以便可以在O(1)时间内完成任何字符的搜索。
- 由长度为N的字符串形成子串的总数是(N *(N + 1))/ 2,INITIALISE计数为(N *(N + 1))/ 2。
- 从左到右遍历字符串,并使用变量lastPos存储我们在字符串中遇到的最后一个字符的索引,该字符在数组arr []中不存在
- 如果遍历字符串遇到arr []中不存在的任何字符,则我们减去将包含该字符且起始点大于lastPos值的子字符串数。假设我们在索引i处,那么要减去的子字符串数将由下式给出:
(i - lastPos)*(N - i)
- 每当遇到charArray []中不可用的字符,更新lastPos的值。
下面是上述方法的实现:
C++
// C++ program for the above approach #include
using namespace std; // Function to find the number of // substrings that can be formed // using given characters void numberofsubstrings(string str, int k, char charArray[]) { int N = str.length(); // Boolean array for storing // the available characters bool available[26] = { 0 }; // Mark indices of all // available characters as 1 for (int i = 0; i < k; i++) { available[charArray[i] - 'a'] = 1; } // Intialize lastPos as -1 int lastPos = -1; // Initialize ans with the total // no of possible substrings int ans = (N * (N + 1)) / 2; // Traverse the string from // left to right for (int i = 0; i < N; i++) { // If the current character // is not present in B if (available[str[i] - 'a'] == 0) { // Subtract the total possible // substrings ans -= ((i - lastPos) * (N - i)); // Update the value of // lastpos to current index lastPos = i; } } // Print the final answer cout << ans << endl; } // Driver Code int main() { // Given String string str = "abcb"; int k = 2; // Given character array char charArray[k] = { 'a', 'b' }; // Function Call numberofsubstrings(str, k, charArray); return 0; }
Java
// Java program for the above approach import java.util.Arrays; class GFG{ // Function to find the number of // substrings that can be formed // using given characters public static void numberofsubstrings(String str, int k, char charArray[]) { int N = str.length(); // Boolean array for storing // the available characters int available[] = new int[26]; Arrays.fill(available, 0); // Mark indices of all // available characters as 1 for(int i = 0; i < k; i++) { available[charArray[i] - 'a'] = 1; } // Initialize lastPos as -1 int lastPos = -1; // Initialize ans with the total // no of possible substrings int ans = (N * (N + 1)) / 2; // Traverse the string from // left to right for(int i = 0; i < N; i++) { // If the current character // is not present in B if (available[str.charAt(i) - 'a'] == 0) { // Subtract the total possible // substrings ans -= ((i - lastPos) * (N - i)); // Update the value of // lastpos to current index lastPos = i; } } // Print the final answer System.out.println(ans); } // Driver Code public static void main(String args[]) { // Given String String str = "abcb"; int k = 2; // Given character array char []charArray = {'a', 'b'}; // Function Call numberofsubstrings(str, k, charArray); } } // This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach # Function to find the number of # substrings that can be formed # using given characters def numberofsubstrings(str, k, charArray): N = len(str) # Boolean array for storing # the available characters available = [0] * 26 # Mark indices of all # available characters as 1 for i in range(0, k): available[ord(charArray[i]) - ord('a')] = 1 # Initialize lastPos as -1 lastPos = -1 # Initialize ans with the total # no of possible substrings ans = (N * (N + 1)) / 2 # Traverse the string from # left to right for i in range(0, N): # If the current character # is not present in B if (available[ord(str[i]) - ord('a')] == 0): # Subtract the total possible # substrings ans -= ((i - lastPos) * (N - i)) # Update the value of # lastpos to current index lastPos = i # Print the final answer print(int(ans)) # Driver Code # Given String str = "abcb" k = 2 # Given character array charArray = [ 'a', 'b' ] # Function call numberofsubstrings(str, k, charArray) # This code is contributed by sanjoy_62
C#
// C# program for the above approach using System; class GFG{ // Function to find the number of // substrings that can be formed // using given characters public static void numberofsubstrings(String str, int k, char []charArray) { int N = str.Length; // Boolean array for storing // the available characters int []available = new int[26]; // Mark indices of all // available characters as 1 for(int i = 0; i < k; i++) { available[charArray[i] - 'a'] = 1; } // Initialize lastPos as -1 int lastPos = -1; // Initialize ans with the total // no of possible substrings int ans = (N * (N + 1)) / 2; // Traverse the string from // left to right for(int i = 0; i < N; i++) { // If the current character // is not present in B if (available[str[i] - 'a'] == 0) { // Subtract the total possible // substrings ans -= ((i - lastPos) * (N - i)); // Update the value of // lastpos to current index lastPos = i; } } // Print the final answer Console.WriteLine(ans); } // Driver Code public static void Main(String []args) { // Given String String str = "abcb"; int k = 2; // Given character array char []charArray = {'a', 'b'}; // Function Call numberofsubstrings(str, k, charArray); } } // This code is contributed by Princi Singh
输出:4
时间复杂度: O(N) ,N是字符串的长度
辅助空间: O(1)