0% found this document useful (0 votes)
4 views2 pages

26 C 35 D 8402 de 5 F 09

The Java program defines a class 'Task1' with a method 'solution' that calculates the maximum number of times words from an array can be formed using the letters from a given string. It counts the occurrences of each letter in the string and each word, comparing them to determine how many complete words can be formed. The main method tests this functionality with a specific string and an array of words, printing the maximum number of words that can be formed.

Uploaded by

pau22360801
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

26 C 35 D 8402 de 5 F 09

The Java program defines a class 'Task1' with a method 'solution' that calculates the maximum number of times words from an array can be formed using the letters from a given string. It counts the occurrences of each letter in the string and each word, comparing them to determine how many complete words can be formed. The main method tests this functionality with a specific string and an array of words, printing the maximum number of words that can be formed.

Uploaded by

pau22360801
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;

public class Task1 {

public int solution(String S, String[] L) {


// Implement your solution here

int max_num = 0;

int[] english_store = new int[26];


Arrays.fill(english_store, 0);

int[] english_test = new int[26];


Arrays.fill(english_test, 0);

char charArray[] = S.toCharArray();


Arrays.sort(charArray);

System.out.println(new String(charArray));

for(int i=0; i<S.length(); i++) {


System.out.println(charArray[i]);
int letter = (int) charArray[i] - 65;
english_store[letter] += 1;
// System.out.println(letter);
}

for (int element: english_store) {


System.out.print(element+",");
}
System.out.println("");

for(int i=0; i<L.length; i++) {


String test = L[i];
char anotherCharArray[] = test.toCharArray();
Arrays.sort(anotherCharArray);
System.out.println(new String(anotherCharArray));

for(int j=0; j<test.length(); j++) {


System.out.println(anotherCharArray[j]);
int letter2 = (int) anotherCharArray[j] - 65;
english_test[letter2] += 1;
// System.out.println(letter2);
}

for (int element: english_test) {


System.out.print(element+",");
}
System.out.println("");

////////////////////////////
int max_test = 0;
int [] store = new int[26];
System.arraycopy(english_store, 0, store, 0, english_store.length);
for (int a=0; a<english_test.length; a++) {

// System.out.print(english_test[a]);
if (english_test[a] == 0)
{
// System.out.print("continue");
continue;
}
else
{
if (store[a] >= english_test[a])
{
int compare = 0;
do
{
store[a] = store[a] - english_test[a];
compare+=1;

}while(store[a] >= english_test[a]);

if(max_test == 0)
{
max_test = compare;
continue;
}
if(compare < max_test)
{
max_test = compare;
}
}
}
}

System.out.println("");
System.out.println("Test:"+max_test);

if(max_test > max_num)


{
max_num = max_test;
}
//////////////////////////////
System.out.println("");
Arrays.fill(english_test, 0);
}

return max_num;
}

public static void main(String[] args) {


// TODO Auto-generated method stub

Task1 t = new Task1();

int n = t.solution("AAAAAAAAAAAABBBB", new String[] {"BBBBB","A", "BAA",


"BAAAA"});
System.out.println("Maximum:"+n);
}

You might also like