![]() |
VOOZH | about |
Given two strings str1 and str2, the task is to find the maximum number of times str1 occurs in str2 as a non-overlapping substring after rearranging the characters of str2
Examples:
Input: str1 = "geeks", str2 = "gskefrgoekees"
Output: 2
str = "geeksforgeeks"
Input: str1 = "aa", str2 = "aaaa"
Output: 2
Approach: The idea is to store the frequency of characters of both the strings and comparing them.
Below is the implementation of the above approach:
2
Time Complexity: O(max(M, N)), as we using a loop to traverse N times and M times. Where M and N are the lengths of the given strings str1 and str2 respectively.
Auxiliary Space: O(26), as we are using extra space for the map.