给定一个整数k和一个字符串str ,任务是计算不同子字符串的数量,使得每个子字符串不包含超过k次的某些特定字符。特定字符作为另一个字符串给出。
例子:
Input: str = “ababab”, anotherStr = “bcd”, k = 1
Output: 5
All valid sub-strings are “a”, “b”, “ab”, “ba” and “aba”
Input: str = “acbacbacaa”, anotherStr = “ycb”, k = 2
Output: 8
方法:
- 将 anotherStr 的字符存储在大小为 256 的布尔数组中以便快速查找
- 遍历给定字符串 的所有子串。对于每个子字符串,在anotherStr 中保留非法字符的数量。
- 如果这些字符的数量超过k的值,则跳出内部循环。
- 否则,将此子字符串存储在哈希表中以保留不同的子字符串。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX_CHAR = 256;
// Function to return the count of valid sub-strings
int countSubStrings(string s, string anotherStr, int k)
{
// Store all characters of anotherStr in a
// direct index table for quick lookup.
bool illegal[MAX_CHAR] = { false };
for (int i = 0; i < anotherStr.size(); i++)
illegal[anotherStr[i]] = true;
// To store distinct output substrings
unordered_set us;
// Traverse through the given string and
// one by one generate substrings beginning
// from s[i].
for (int i = 0; i < s.size(); ++i) {
// One by one generate substrings ending
// with s[j]
string ss = "";
int count = 0;
for (int j = i; j < s.size(); ++j) {
// If character is illegal
if (illegal[s[j]])
++count;
ss = ss + s[j];
// If current substring is valid
if (count <= k) {
us.insert(ss);
}
// If current substring is invalid,
// adding more characters would not
// help.
else
break;
}
}
// Return the count of distinct sub-strings
return us.size();
}
// Driver code
int main()
{
string str = "acbacbacaa";
string anotherStr = "abcdefghijklmnopqrstuvwxyz";
int k = 2;
cout << countSubStrings(str, anotherStr, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG {
static int MAX_CHAR = 256;
// Function to return the count of valid sub-strings
static int countSubStrings(String s, String anotherStr, int k)
{
// Store all characters of anotherStr in a
// direct index table for quick lookup.
boolean illegal[] = new boolean[MAX_CHAR];
for (int i = 0; i < anotherStr.length(); i++)
{
illegal[anotherStr.charAt(i)] = true;
}
// To store distinct output substrings
HashSet us = new HashSet();
// Traverse through the given string and
// one by one generate substrings beginning
// from s[i].
for (int i = 0; i < s.length(); ++i)
{
// One by one generate substrings ending
// with s[j]
String ss = "";
int count = 0;
for (int j = i; j < s.length(); ++j)
{
// If character is illegal
if (illegal[s.charAt(j)])
{
++count;
}
ss = ss + s.charAt(j);
// If current substring is valid
if (count <= k)
{
us.add(ss);
}
// If current substring is invalid,
// adding more characters would not
// help.
else
{
break;
}
}
}
// Return the count of distinct sub-strings
return us.size();
}
// Driver code
public static void main(String[] args)
{
String str = "acbacbacaa";
String anotherStr = "abcdefghijklmnopqrstuvwxyz";
int k = 2;
System.out.println(countSubStrings(str, anotherStr, k));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
MAX_CHAR = 256
# Function to return the count
# of valid sub-strings
def countSubStrings(s, anotherStr, k) :
# Store all characters of anotherStr in
# a direct index table for quick lookup.
illegal = [False] * MAX_CHAR
for i in range(len(anotherStr)) :
illegal[ord(anotherStr[i])] = True
# To store distinct output substrings
us = set()
# Traverse through the given string
# and one by one generate substrings
# beginning from s[i].
for i in range(len(s)) :
# One by one generate substrings
# ending with s[j]
ss = ""
count = 0
for j in range(i, len(s)) :
# If character is illegal
if (illegal[ord(s[j])]) :
count += 1
ss = ss + s[j]
# If current substring is valid
if (count <= k) :
us.add(ss)
# If current substring is invalid,
# adding more characters would not
# help.
else :
break
# Return the count of distinct
# sub-strings
return len(us)
# Driver code
if __name__ == "__main__" :
string = "acbacbacaa"
anotherStr = "abcdefghijklmnopqrstuvwxyz"
k = 2
print(countSubStrings(string,
anotherStr, k))
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
static int MAX_CHAR = 256;
// Function to return the count
// of valid sub-strings
static int countSubStrings(String s,
String anotherStr, int k)
{
// Store all characters of anotherStr in a
// direct index table for quick lookup.
bool []illegal = new bool[MAX_CHAR];
for (int i = 0; i < anotherStr.Length; i++)
{
illegal[anotherStr[i]] = true;
}
// To store distinct output substrings
HashSet us = new HashSet();
// Traverse through the given
// string and one by one generate
// substrings beginning from s[i].
for (int i = 0; i < s.Length; ++i)
{
// One by one generate substrings
// ending with s[j]
String ss = "";
int count = 0;
for (int j = i; j < s.Length; ++j)
{
// If character is illegal
if (illegal[s[j]])
{
++count;
}
ss = ss + s[j];
// If current substring is valid
if (count <= k)
{
us.Add(ss);
}
// If current substring is invalid,
// adding more characters would not
// help.
else
{
break;
}
}
}
// Return the count of distinct sub-strings
return us.Count;
}
// Driver code
public static void Main()
{
String str = "acbacbacaa";
String anotherStr = "abcdefghijklmnopqrstuvwxyz";
int k = 2;
Console.WriteLine(countSubStrings(str, anotherStr, k));
}
}
//This code is contributed by 29AjayKumar
Javascript
输出:
8
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。