Length of longest prefix anagram which are common in given two strings
Last Updated :
28 Dec, 2022
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 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++
#include <bits/stdc++.h>
using namespace std;
#define SIZE 26
bool longHelper( int freq1[], int freq2[])
{
for ( int i = 0; i < SIZE; ++i) {
if (freq1[i] != freq2[i]) {
return false ;
}
}
return true ;
}
int longCommomPrefixAnagram(
string s1, string s2, int n1, int n2)
{
int freq1[26] = { 0 };
int freq2[26] = { 0 };
int ans = 0;
int mini_len = min(n1, n2);
for ( int i = 0; i < mini_len; ++i) {
freq1[s1[i] - 'a' ]++;
freq2[s2[i] - 'a' ]++;
if (longHelper(freq1, freq2)) {
ans = i + 1;
}
}
cout << ans;
}
int main()
{
string str1 = "abaabcdezzwer" ;
string str2 = "caaabbttyh" ;
int N = str1.length();
int M = str2.length();
longCommomPrefixAnagram(str1, str2,
N, M);
return 0;
}
|
Java
public class Main
{
static int SIZE = 26 ;
static boolean longHelper( int [] freq1, int [] freq2)
{
for ( int i = 0 ; i < SIZE; ++i)
{
if (freq1[i] != freq2[i])
{
return false ;
}
}
return true ;
}
static void longCommomPrefixAnagram(
String s1, String s2, int n1, int n2)
{
int [] freq1 = new int [ 26 ];
int [] freq2 = new int [ 26 ];
int ans = 0 ;
int mini_len = Math.min(n1, n2);
for ( int i = 0 ; i < mini_len; ++i) {
freq1[s1.charAt(i) - 'a' ]++;
freq2[s2.charAt(i) - 'a' ]++;
if (longHelper(freq1, freq2)) {
ans = i + 1 ;
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
String str1 = "abaabcdezzwer" ;
String str2 = "caaabbttyh" ;
int N = str1.length();
int M = str2.length();
longCommomPrefixAnagram(str1, str2, N, M);
}
}
|
Python3
SIZE = 26
def longHelper(freq1, freq2):
for i in range ( 26 ):
if (freq1[i] ! = freq2[i]):
return False
return True
def longCommomPrefixAnagram(s1, s2, n1, n2):
freq1 = [ 0 ] * 26
freq2 = [ 0 ] * 26
ans = 0
mini_len = min (n1, n2)
for i in range (mini_len):
freq1[ ord (s1[i]) - ord ( 'a' )] + = 1
freq2[ ord (s2[i]) - ord ( 'a' )] + = 1
if (longHelper(freq1, freq2)):
ans = i + 1
print (ans)
if __name__ = = '__main__' :
str1 = "abaabcdezzwer"
str2 = "caaabbttyh"
N = len (str1)
M = len (str2)
longCommomPrefixAnagram(str1, str2, N, M)
|
C#
using System;
class GFG
{
static int SIZE = 26;
static bool longHelper( int [] freq1, int [] freq2)
{
for ( int i = 0; i < SIZE; ++i)
{
if (freq1[i] != freq2[i])
{
return false ;
}
}
return true ;
}
static void longCommomPrefixAnagram(
string s1, string s2, int n1, int n2)
{
int [] freq1 = new int [26];
int [] freq2 = new int [26];
int ans = 0;
int mini_len = Math.Min(n1, n2);
for ( int i = 0; i < mini_len; ++i) {
freq1[s1[i] - 'a' ]++;
freq2[s2[i] - 'a' ]++;
if (longHelper(freq1, freq2)) {
ans = i + 1;
}
}
Console.Write(ans);
}
static void Main() {
string str1 = "abaabcdezzwer" ;
string str2 = "caaabbttyh" ;
int N = str1.Length;
int M = str2.Length;
longCommomPrefixAnagram(str1, str2, N, M);
}
}
|
Javascript
<script>
let SIZE = 26;
function longHelper(freq1, freq2)
{
for (let i = 0; i < SIZE; ++i)
{
if (freq1[i] != freq2[i])
{
return false ;
}
}
return true ;
}
function longCommomPrefixAnagram(s1, s2, n1, n2)
{
let freq1 = new Array(26);
freq1.fill(0);
let freq2 = new Array(26);
freq2.fill(0);
let ans = 0;
let mini_len = Math.min(n1, n2);
for (let i = 0; i < mini_len; ++i)
{
freq1[s1[i].charCodeAt() -
'a' .charCodeAt()]++;
freq2[s2[i].charCodeAt() -
'a' .charCodeAt()]++;
if (longHelper(freq1, freq2))
{
ans = i + 1;
}
}
document.write(ans);
}
let str1 = "abaabcdezzwer" ;
let str2 = "caaabbttyh" ;
let N = str1.length;
let M = str2.length;
longCommomPrefixAnagram(str1, str2, N, M);
</script>
|
Time Complexity: O(N*26)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...