Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// C++ Program to find the size of 
// largest subset of anagram 
#include <bits/stdc++.h> 
using namespace std; 

// Utility function to find size of 
// largest subset of anagram 
int largestAnagramSet(string arr[], int n) 
{ 
	int maxSize = 0; 

	// Initialize map<> of vector array 
	map<vector<int>, int> count; 

	for (int i = 0; i < n; ++i) { 

		// Vector array to store 
		// frequency of element 
		vector<int> freq(26); 

		for (char ch : arr[i]) 
			freq[ch - 'a'] += 1; 

		// Increment the count of 
		// frequency array in map<> 
		count[freq] += 1; 

		// Compute the maximum size 
		maxSize = max(maxSize, count[freq]); 
	} 
	return maxSize; 
} 

// Driver code 
int main() 
{ 
	string arr[] = { "ant", "magenta", "magnate", 
							"tan", "gnamate" }; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	cout << largestAnagramSet(arr, n) << "\n"; 

	string arr1[] = { "cars", "bikes", "arcs", 
									"steer" }; 
	n = sizeof(arr1) / sizeof(arr[0]); 
	cout << largestAnagramSet(arr1, n); 
	return 0; 
} 


What I have tried:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hw1;

import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author rafeaaalbloushi
 */
public class anagram {
    public int largestAnagramSet(String arr[], int n) 
{ 

	int maxSize = 0; 
	ArrayList<String, int>=new ArrayList; 

	for (int i = 0; i < n; ++i) { 

		// sort the string 
		Arrays.sort(a, maxSize, i);

		// Increment the count of string 
		count[arr[i]] += 1; 

		// Compute the maximum size of string 
		maxSize = max(maxSize, count[arr[i]]); 
	} 

	return maxSize; 
} 
}
Posted
Updated 7-Dec-20 4:56am
Comments
OriginalGriff 27-Sep-19 10:04am    
And?
What does it do that you didn't expect, or not do that you did?
ZurdoDev 27-Sep-19 10:42am    
Where are you stuck?
rafeaa27 27-Sep-19 12:03pm    
it gives errors when I try to run it in java

You can't run it in Java since it is not complete. You need to add the following:
Java
public static void main(String[] argv) {
   // create an instance of the anagram class and call its methods here
}
 
Share this answer
 
Your Java code doesn't reproduce the C++ code behaviour.
Try the followng (please note my Java is rusty, I believe there are more elegant ways to write such code)
Java
import java.util.*;
class MyClass
{
  public static Integer largestAnagramSet( String [] arr )
  {
    Integer maxSize = 0;
    Comparator<ArrayList < Integer > > alComparator = new Comparator<ArrayList< Integer> >()
    {
        @Override public int compare( ArrayList< Integer> a1, ArrayList <Integer> a2)
        {
          for ( int n = 0; n < a1.size(); ++n)
            if ( a1.get(n) > a2.get(n))
              return 1;
            else if ( a1.get(n) < a2.get(n) )
              return -1;
          return 0;
        }
    };
    Map < ArrayList< Integer >, Integer > count = new TreeMap < ArrayList < Integer >, Integer>(alComparator) ;
    Integer occurrences;
    for ( int i = 0; i < arr.length; ++i)
    {
      ArrayList < Integer > freq = new ArrayList< Integer>();
      for (int j = 0; j < 26; ++j)
        freq.add(0);
      for (int j=0; j<arr[i].length(); ++j)
      {
        int index = arr[i].charAt(j) -'a';
        freq.set(index, freq.get(index) + 1);
      }

      occurrences = 0;
      if ( count.containsKey( freq) )
      {
        occurrences = count.get(freq);
      }
      ++occurrences;
      count.put( freq, occurrences);

      if ( maxSize < occurrences )
        maxSize = occurrences;
    }
    return maxSize;
  }

  public static void main(String [] arg)
  {
    String [] arr = { "ant", "magenta", "magnate", "tan", "gnamate" };
    System.out.println( largestAnagramSet(arr).toString());
    String arr1[] = { "cars", "bikes", "arcs", "steer" };
    System.out.println( largestAnagramSet(arr1).toString());
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900