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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of total anagram substrings
- Minimum number of adjacent swaps to convert a string into its given anagram
- Number of substrings of a string
- Number of even substrings in a string of digits
- Number of substrings of one string present in other
- Sum of all substrings of a string representing a number | Set 1
- Number of substrings divisible by 6 in a string of integers
- Number of substrings with odd decimal value in a binary string
- Split the binary string into substrings with equal number of 0s and 1s
- Rearrange the string to maximize the number of palindromic substrings
- Count the number of vowels occurring in all the substrings of given string
- Count number of substrings of a string consisting of same characters
- Repeat substrings of the given String required number of times
- Given a binary string, count number of substrings that start and end with 1.
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Convert string X to an anagram of string Y with minimum replacements
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum number of steps needed to remove the substring K from given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.