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.
Input: S1 = “ABB”, S2 = “BAB”
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”
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count substrings of a given string whose anagram is a palindrome
- Find length of longest subsequence of one string which is substring of another string
- Length of smallest substring of a given string which contains another string as subsequence
- Count of total anagram substrings
- Check if any anagram of a string is palindrome or not
- Anagram Substring Search (Or Search for all permutations)
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Replace even-indexed characters of minimum number of substrings to convert a string to another
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Length of longest substring to be deleted to make a string equal to another string
- Check if the given string is shuffled substring of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Convert string X to an anagram of string Y with minimum replacements
- Removing string that is an anagram of an earlier string
- Smallest non-zero substring which has any permutation divisible by 2^K
- Check if a string can be transformed to another by sorting substrings
- Minimum number of adjacent swaps to convert a string into its given anagram
- Minimum substring flips required to convert given binary string to another
- Check if a string is substring of another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.