Length of longest prefix anagram which are common in given two strings


In this article, we delve into a fascinating problem in the realm of string manipulation and anagram analysis. Specifically, we'll be finding the length of the longest prefix anagram that is common to two given strings. Our solution leverages C, C++, Java and Python, a powerful and versatile programming languages beloved by software developers.

Understanding Anagrams

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For instance, the words 'listen' and 'silent' are anagrams of each other.

Problem Statement

Given two strings, we need to find the length of the longest prefix anagram present in both strings. A prefix is a substring that starts from the first character of a string.

Solution Approach

To solve this problem, we'll follow these steps −

  • Initialization − Initialize an integer array of size 26 (representing the 26 English alphabets) for each string. These arrays will store the frequency count of each character in the prefix of the strings.

  • Frequency Counting − For each character in the string's prefix, increment the corresponding index in the array.

  • Comparison − Compare the frequency arrays of the two strings. Find the minimum frequency of each character, and sum these frequencies.

  • Return Result − The sum is the length of the longest common anagram prefix.

Implementation

Example

Here're the programs for our problem solution −

#include <stdio.h>
#include <string.h>

int longestCommonPrefixAnagram(char str1[], char str2[]) {
   int n1 = strlen(str1), n2 = strlen(str2);
   int count1[26] = {0}, count2[26] = {0};
   
   for (int i = 0; i < n1; i++)
      count1[str1[i] - 'a']++;
   for (int i = 0; i < n2; i++)
      count2[str2[i] - 'a']++;
   
   int result = 0;
   for (int i = 0; i < 26; i++)
      result += (count1[i] < count2[i] ? count1[i] : count2[i]);
   
   return result;
}

int main() {
   char str1[] = "abcdef";
   char str2[] = "fedcba";
   
   int result = longestCommonPrefixAnagram(str1, str2);
   printf("Length of longest common prefix anagram: %d\n", result);
   
   return 0;
}

Output

Length of longest common prefix anagram: 6
#include <bits/stdc++.h>
using namespace std;

int longestCommonPrefixAnagram(string str1, string str2) {
   int n1 = str1.length(), n2 = str2.length();
   vector<int> count1(26, 0), count2(26, 0);
   
   for (int i = 0; i < n1; i++)
      count1[str1[i] - 'a']++;
   for (int i = 0; i < n2; i++)
      count2[str2[i] - 'a']++;
   
   int result = 0;
   for (int i = 0; i < 26; i++)
      result += min(count1[i], count2[i]);
   
   return result;
}

int main() {
   string str1 = "abcdef";
   string str2 = "fedcba";
   
   int result = longestCommonPrefixAnagram(str1, str2);
   cout << "Length of longest common prefix anagram: " << result << endl;
   
   return 0;
}

Output

Length of longest common prefix anagram: 6
import java.util.Arrays;

public class Main {
   public static int longestCommonPrefixAnagram(String str1, String str2) {
      int n1 = str1.length();
      int n2 = str2.length();
      int[] count1 = new int[26];
      int[] count2 = new int[26];

      for (int i = 0; i < n1; i++) {
         count1[str1.charAt(i) - 'a']++;
      }

      for (int i = 0; i < n2; i++) {
         count2[str2.charAt(i) - 'a']++;
      }

      int result = 0;
      for (int i = 0; i < 26; i++) {
         result += Math.min(count1[i], count2[i]);
      }

      return result;
   }

   public static void main(String[] args) {
      String str1 = "abcdef";
      String str2 = "fedcba";

      int result = longestCommonPrefixAnagram(str1, str2);
      System.out.println("Length of longest common prefix anagram: " + result);
   }
}

Output

Length of longest common prefix anagram: 6
def longest_common_prefix_anagram(str1, str2):
   n1 = len(str1)
   n2 = len(str2)
   count1 = [0] * 26
   count2 = [0] * 26

   for i in range(n1):
      count1[ord(str1[i]) - ord('a')] += 1

   for i in range(n2):
      count2[ord(str2[i]) - ord('a')] += 1

   result = 0
   for i in range(26):
      result += min(count1[i], count2[i])

   return result

if __name__ == "__main__":
   str1 = "abcdef"
   str2 = "fedcba"

   result = longest_common_prefix_anagram(str1, str2)
   print("Length of longest common prefix anagram:", result)

Output

Length of longest common prefix anagram: 6

Explanation

Let's consider the strings −

str1 = "abcdef", str2 = "fedcba"

After counting the frequencies of each character, we find that both strings contain exactly the same characters (albeit in a different order). Hence, the longest common prefix anagram is the entire string, and its length is 6, which is the output of our program.

Conclusion

The problem of finding the length of the longest prefix anagram common in two strings is an interesting one that tests our understanding of string manipulation and frequency counting. This solution illustrates how we can use basic C, C++, Java and Python programming constructs to solve it efficiently.

Updated on: 23-Oct-2023

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements