[ad_1]
// 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; }
私が試したこと:
/* * 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; } }
解決策 1
完全ではないため、Java で実行することはできません。 以下を追加する必要があります。
Java
public static void main(String[] argv) { // create an instance of the anagram class and call its methods here }
解決策 2
あなたの Java コードは C++
コードの振る舞い。
以下を試してください(私の注意してください Java
は錆びていますが、そのようなコードを書くにはもっとエレガントな方法があると思います)
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()); } }
[ad_2]
コメント