Given two strings str1 and str2 of the lengths of N and M respectively, the task is to find the length of the longest anagram string that is prefix substring of both the strings.
Examples:
Input: str1 = “abaabcdezzwer”, str2 = “caaabbttyh”
Output: 6
Explanation:
Prefixes of length 1 of string str1 and str2 are “a”, and “c”.
Prefixes of length 2 of string str1 and str2 are “ab”, and “ca”.
Prefixes of length 3 of string str1 and str2 are “aba”, and “caa”.
Prefixes of length 4 of string str1 and str2 are “abaa”, and “caaa”.
Prefixes of length 5 of string str1 and str2 are “abaab”, and “caaab”.
Prefixes of length 6 of string str1 and str2 are “abaabc”, and “caaabb”.
Prefixes of length 7 of string str1 and str2 are “abaabcd”, and “caaabbt”.
Prefixes of length 8 of string str1 and str2 are “abaabcde”, and “caaabbtt”.
Prefixes of length 9 of string str1 and str2 are “abaabcdez”, and “caaabbtty”.
Prefixes of length 10 of string str1 and str2 are “abaabcdezz”, and “caaabbttyh”.
Prefixes of length 6 are anagram with each other only.Input: str1 = “abcdef”, str2 = “tuvwxyz”
Output: 0
Approach: The idea is to use Hashing for solving the above problem. Follow the steps below to solve the problem:
- Initialize two integer arrays freq1[] and freq2[], each of size 26, to store the count of characters in strings str1 and str2 respectively.
- Initialize a variable, say ans, to store the result.
- Iterate over the characters of the string present in indices [0, minimum(N – 1, M – 1)] and perform the following:
- Increment count of str1[i] in freq1[] array and count of str2[i] in freq2[] array by 1.
- Check if the frequency array freq1[] is the same as the frequency array freq2[], assign ans = i + 1.
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; #define SIZE 26 // Function to check if two arrays // are identical or not bool longHelper(int freq1[], int freq2[]) { // Iterate over the range [0, SIZE] for (int i = 0; i < SIZE; ++i) { // If frequency any character is // not same in both the strings if (freq1[i] != freq2[i]) { return false; } } // Otherwise return true; } // Function to find the maximum // length of the required string int longCommomPrefixAnagram( string s1, string s2, int n1, int n2) { // Store the count of // characters in string str1 int freq1[26] = { 0 }; // Store the count of // characters in string str2 int freq2[26] = { 0 }; // Stores the maximum length int ans = 0; // Minimum length of str1 and str2 int mini_len = min(n1, n2); for (int i = 0; i < mini_len; ++i) { // Increment the count of // characters of str1[i] in // freq1[] by one freq1[s1[i] - 'a']++; // Increment the count of // characters of str2[i] in // freq2[] by one freq2[s2[i] - 'a']++; // Checks if prefixes are // anagram or not if (longHelper(freq1, freq2)) { ans = i + 1; } } // Finally print the ans cout << ans; } // Driver Code int main() { string str1 = "abaabcdezzwer"; string str2 = "caaabbttyh"; int N = str1.length(); int M = str2.length(); // Function Call longCommomPrefixAnagram(str1, str2, N, M); return 0; } |
6
Time Complexity: O(N*26)
Auxiliary Space: O(26)
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.

