给定两个字符串S1和S2 ,任务是计算S1的子字符串的数目,这些子字符串是S2的任何子字符串的字谜。
例子:
Input: S1 = “ABB”, S2 = “BAB”
Output: 5
There are 6 sub-strings of S1 : “A”, “B”, “B”, “AB”, “BB” and “ABB”
Out of which only “BB” is the one which is not an anagram of any sub-string of S2.
Input: S1 = “PLEASEHELPIMTRAPPED”, S2 = “INAKICKSTARTFACTORY”
Output: 9
天真的方法:一种简单的方法是对照S2的所有子串检查S1的所有子串是否为字谜。
有效的方法:由一个临时说拿一个S1的所有子串,并检查是否温度是S2的任意子串的字谜,通过计算温度的所有字符的频率,并将其与子的字符比较频率S2的字符串,其长度为length = length(temp) 。
可以通过获取S2的第一个length(temp)字符来进行一次遍历,然后对于每次迭代,添加字符串的下一个字符的频率,并删除先前选择的子字符串的第一个字符的频率直到遍历完整的字符串。
下面是上述方法的实现:
C++
// C++ program to find the number of sub-strings
// of s1 which are anagram of any sub-string of s2
#include
using namespace std;
#define ALL_CHARS 256
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
bool compare(char* arr1, char* arr2)
{
for (int i = 0; i < ALL_CHARS; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all permutations
// of string pat[] in string txt[]
bool search(string pat, string txt)
{
int M = pat.length();
int N = txt.length();
int i;
// countP[]: Store count of all characters
// of pattern
// countTW[]: Store count of current
// window of text
char countP[ALL_CHARS] = { 0 };
char countTW[ALL_CHARS] = { 0 };
for (i = 0; i < M; i++) {
(countP[pat[i]])++;
(countTW[txt[i]])++;
}
// Traverse through remaining
// characters of pattern
for (i = M; i < N; i++) {
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW)) {
// cout<
Java
// Java program to find the number of sub-Strings
// of s1 which are anagram of any sub-String of s2
class GFG {
static int MAX_LEN = 1005;
static int MAX_CHAR = 26;
static int ALL_CHARS = 256;
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
static boolean compare(char[] arr1, char[] arr2)
{
for (int i = 0; i < ALL_CHARS; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all permutations
// of String pat[] in String txt[]
static boolean search(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int i;
// countP[]: Store count of all characters
// of pattern
// countTW[]: Store count of current
// window of text
char countP[] = new char[ALL_CHARS];
char countTW[] = new char[ALL_CHARS];
for (i = 0; i < M; i++) {
(countP[pat.charAt(i)])++;
(countTW[txt.charAt(i)])++;
}
// Traverse through remaining
// characters of pattern
for (i = M; i < N; i++) {
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW)) {
// cout<
Python3
# Python3 program to find the number of sub-strings
# of s1 which are anagram of any sub-string of s2
ALL_CHARS = 256
# This function returns true if
# contents of arr1[] and arr2[]
# are same, otherwise false.
def compare(arr1, arr2):
for i in range(ALL_CHARS):
if arr1[i] != arr2[i]:
return False
return True
# This function search for all permutations
# of string pat[] in string txt[]
def search(pat, txt):
M = len(pat)
N = len(txt)
# countP[]: Store count of all characters
# of pattern
# countTW[]: Store count of current
# window of text
countP = [0] * ALL_CHARS
countTW = [0] * ALL_CHARS
for i in range(M):
countP[ord(pat[i])] += 1
countTW[ord(txt[i])] += 1
# Traverse through remaining
# characters of pattern
for i in range(M, N):
# Compare counts of current
# window of text with
# counts of pattern[]
if compare(countP, countTW):
return True
# Add current character to current window
countTW[ord(txt[i])] += 1
# Remove the first character
# of previous window
countTW[ord(txt[i - M])] -= 1
# Check for the last window in text
if compare(countP, countTW):
return True
return False
# Function to return the number of sub-strings of s1
# that are anagrams of any sub-string of s2
def calculateSubString(s1, s2, n):
# initializing variables
count, j, x = 0, 0, 0
# outer loop for picking starting point
for i in range(n):
# loop for different length of substrings
for length in range(1, n - i + 1):
# If s2 has any substring which is
# anagram of s1.substr(i, len)
if search(s1[i:i + length], s2):
# increment the count
count += 1
return count
# Driver Code
if __name__ == "__main__":
str1 = "PLEASEHELPIMTRAPPED"
str2 = "INAKICKSTARTFACTORY"
length = len(str1)
print(calculateSubString(str1, str2, length))
# This code is contributed by
# sanjeev2552
C#
// C# program to find the number of sub-Strings
// of s1 which are anagram of any sub-String of s2
using System;
using System.Collections.Generic;
class GFG {
static int MAX_LEN = 1005;
static int MAX_CHAR = 26;
static int ALL_CHARS = 256;
// This function returns true if
// contents of arr1[] and arr2[]
// are same, otherwise false.
static bool compare(char[] arr1, char[] arr2)
{
for (int i = 0; i < ALL_CHARS; i++)
if (arr1[i] != arr2[i])
return false;
return true;
}
// This function search for all permutations
// of String pat[] in String txt[]
static bool search(String pat, String txt)
{
int M = pat.Length;
int N = txt.Length;
int i;
// countP[]: Store count of all characters
// of pattern
// countTW[]: Store count of current
// window of text
char[] countP = new char[ALL_CHARS];
char[] countTW = new char[ALL_CHARS];
for (i = 0; i < M; i++) {
(countP[pat[i]])++;
(countTW[txt[i]])++;
}
// Traverse through remaining
// characters of pattern
for (i = M; i < N; i++) {
// Compare counts of current
// window of text with
// counts of pattern[]
if (compare(countP, countTW)) {
// cout<
输出:
9