Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Thanks for the quick response. Below is my struggled code. You can think of \"n\

ID: 3626540 • Letter: T

Question

Thanks for the quick response. Below is my struggled code. You can think of "n" as the group size and "k" teams. I need to have the output be the combinations of the String "ABCDE" of 5 choose 3. So "ABC ABD ABE and so on nCk times which will have ten outputs. Having two different recursion calls in the method makes it really hard for me to follow.

static String theString = "ABCDE";
static StringBuffer buildString = new StringBuffer();

public static int showTeams(int n, int k, StringBuffer current)
{
int size = theString.length();
int hard;
StringBuffer temp = current;
int length;
length = temp.length();
char ch;

// Base Cases

if( n == 0 || k == 0 || k > n )
{

return 0;
}

else
{
ch = theString.charAt(size - n);

if(length < 3)
temp.append(ch);

if(k==1)
{
if(length == 2)
System.out.println(temp);
if(k==1 && length == 2)
temp = temp.deleteCharAt(2);
}

hard = showTeams(n-1, k-1, temp) + showTeams(n-1, k, temp);

return hard;

}
}

Explanation / Answer

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StringCombinations {
   
    /*
    * Recursive method to return a set of all combinations of size k from a group
    */
    public static List<List<Character>> combinations(List<Character> group, int k) {
        List<List<Character>> allCombos = new ArrayList<List<Character>> ();
       
        // base cases for recursion
        if (k == 0) {
            // There is only one combination of size 0, the empty team.
            allCombos.add(new ArrayList<Character>());
            return allCombos;
        }
        if (k > group.size()) {
            // There can be no teams with size larger than the group size,
            // so return allCombos without putting any teams in it.
            return allCombos;
        }
       
        // Create a copy of the group with one item removed.
        List<Character> groupWithoutX = new ArrayList<Character> (group);
        Character x = groupWithoutX.remove(groupWithoutX.size()-1);
       
        List<List<Character>> combosWithoutX = combinations(groupWithoutX, k);
        List<List<Character>> combosWithX = combinations(groupWithoutX, k-1);
        for (List<Character> combo : combosWithX) {
            combo.add(x);
        }
        allCombos.addAll(combosWithoutX);
        allCombos.addAll(combosWithX);
        return allCombos;
    }
   
    public static void showteams(String str, int k) {
        List<Character> group = new ArrayList<Character>();
        for (int i=0; i<str.length(); i++) {
            group.add(str.charAt(i));
        }
        for (List<Character> team : combinations(group,k)) {
            for (Character member : team) {
                System.out.print(member);
            }
            System.out.print(" ");
        }
    }
   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter string to find combinations of (for example, ABCDE): ");
        String s = in.nextLine();
        System.out.print("Enter number for team size: ");
        int k = in.nextInt();
        in.nextLine();
        System.out.println("Here are all possible teams.");
        showteams(s,k);
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote