Number of sub-strings which are anagram of any sub-string of another string
Given two strings S1 and S2, the task is to count the number of sub-strings of S1 that are anagrams of any sub-string of S2.
Examples:
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
Naive approach: A simple approach is to check all the sub-strings of S1 against all the sub-strings of S2 whether they are anagrams or not.
Efficient approach: Take all the sub-strings of S1 one by one say temp and check whether temp is an anagram of any sub-string of S2 by calculating the frequencies of all the characters of temp and comparing it with the character frequencies of sub-strings of S2 having length = length(temp).
This can be done with a single traversal by taking the first length(temp) characters of S2 and then for every iteration, add the frequency of the next character of the string and remove the frequency of the first character of the previously chosen sub-string until the complete string is traversed.
Below is the implementation of the above approach:
C++
// C++ program to find the number of sub-strings // of s1 which are anagram of any sub-string of s2 #include <bits/stdc++.h> 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<<pat<<" "<<txt<<" "; return true; } // Add current character to current window (countTW[txt[i]])++; // Remove the first character // of previous window countTW[txt[i - M]]--; } // 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 int calculatesubString(string s1, string s2, int n) { // initializing variables int count = 0, j = 0, x = 0; // outer loop for picking starting point for (int i = 0; i < n; i++) { // loop for different length of substrings for (int len = 1; len <= n - i; len++) { // If s2 has any substring which is // anagram of s1.substr(i, len) if (search(s1.substr(i, len), s2)) { // increment the count count = count + 1; } } } return count; } // Driver code int main() { string str1 = "PLEASEHELPIMTRAPPED"; string str2 = "INAKICKSTARTFACTORY"; int len = str1.length(); cout << calculatesubString(str1, str2, len); return 0; } |
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<<pat<<" "<<txt<<" "; return true; } // Add current character to current window (countTW[txt.charAt(i)])++; // Remove the first character // of previous window countTW[txt.charAt(i - M)]--; } // 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 static int calculatesubString(String s1, String s2, int n) { // initializing variables int count = 0, j = 0, x = 0; // outer loop for picking starting point for (int i = 0; i < n; i++) { // loop for different length of subStrings for (int len = 1; len <= n - i; len++) { // If s2 has any subString which is // anagram of s1.substr(i, len) if (search(s1.substring(i, i + len), s2)) { // increment the count count = count + 1; } } } return count; } // Driver code public static void main(String args[]) { String str1 = "PLEASEHELPIMTRAPPED"; String str2 = "INAKICKSTARTFACTORY"; int len = str1.length(); System.out.println(calculatesubString(str1, str2, len)); } } // This code has been contributed by 29AjayKumar |
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<<pat<<" "<<txt<<" "; return true; } // Add current character to current window (countTW[txt[i]])++; // Remove the first character // of previous window countTW[txt[i - M]]--; } // 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 static int calculatesubString(String s1, String s2, int n) { // initializing variables int count = 0, j = 0, x = 0; // outer loop for picking starting point for (int i = 0; i < n; i++) { // loop for different length of subStrings for (int len = 1; len <= n - i; len++) { // If s2 has any subString which is // anagram of s1.substr(i, len) if (search(s1.Substring(i, len), s2)) { // increment the count count = count + 1; } } } return count; } // Driver code public static void Main(String[] args) { String str1 = "PLEASEHELPIMTRAPPED"; String str2 = "INAKICKSTARTFACTORY"; int len = str1.Length; Console.WriteLine(calculatesubString(str1, str2, len)); } } /* This code contributed by PrinciRaj1992 */ |
9
Recommended Posts:
- Count of total anagram substrings
- Minimum number of adjacent swaps to convert a string into its given anagram
- Number of substrings of a string
- Sum of all substrings of a string representing a number | Set 1
- Number of substrings of one string present in other
- Number of even substrings in a string of digits
- Number of substrings divisible by 6 in a string of integers
- Number of substrings with odd decimal value in a binary string
- Rearrange the string to maximize the number of palindromic substrings
- Repeat substrings of the given String required number of times
- Count the number of vowels occurring in all the substrings of given string
- Given a binary string, count number of substrings that start and end with 1.
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Covert string X to an anagram of string Y with minimum replacements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



