jordaniac89
jordaniac89

Reputation: 574

Get all possible name combinations from String array?

I have some code that takes a string of names that has already been delimited by periods (nameStr) and break it apart into a string array:

public static String doc;

public static void buildNameChoices (String whichName, String nameStr)
{
    String[] nameArray= nameStr.split("\\.", -1);

    for(int i=0; i < (nameArray.length); i++){
        nameArray[i]= nameArray[i]+".";
        System.out.println(nameArray[i]);
    }

The second part adds the periods back into each array element, because they are going to be concatenated back together in the output. I'm trying to use this to create a list of all possible name combinations that a user can choose from. The only thing we don't have to worry about is that a higher element can't come before a lower (e.g. the 2nd element wouldn't come first and the 1st second, etc). In other words, it would be "n" and then all possible combinations of what comes after "n".

So, if I had the string, "von.del.smith.john.james", selections could be:

von.del

von.smith

von.john

von.james

von.del.smith

von.smith.john

von.smith.james

etc, etc.

I'm pretty stuck on how to do this. Algorithms aren't my forte.

Upvotes: 1

Views: 1885

Answers (2)

irrelephant
irrelephant

Reputation: 4111

This problem can be solved with recursion. One possible implementation would look like:

public static void buildNameChoicesHelper(String[] nameArray, int nameIndex,
    String str) {

  if(nameIndex >= nameArray.length) {
    if(str.length() > 0) {
      System.out.println(str.substring(0, str.length() - 1));
    }
  }
  else {
    buildNameChoicesHelper(nameArray, nameIndex + 1, str);
    buildNameChoicesHelper(nameArray, nameIndex + 1, str + nameArray[nameIndex] + ".");
  }
}
public static void buildNameChoices(String nameStr) {
  String[] nameArray = nameStr.split("\\.", -1);
  buildNameChoicesHelper(nameArray, 0, "");
}

If every name must be a (first name, last name) combination, then you could try this modification:

public static void buildNameChoicesHelper(String[] nameArray, int nameIndex,
    String firstName, String lastName) {

  if(nameIndex >= nameArray.length) {
    if(lastName.length() > 0) {
      System.out.println(firstName + lastName);
    }
  }
  else {
    buildNameChoicesHelper(nameArray, nameIndex + 1, firstName, lastName);
    buildNameChoicesHelper(nameArray, nameIndex + 1, firstName, lastName + "." + nameArray[nameIndex]);
  }
}
public static void buildNameChoices(String nameStr) {
  String[] nameArray = nameStr.split("\\.", -1);
  for(int i = 0; i < nameArray.length; i++) {
    buildNameChoicesHelper(nameArray, i + 1, nameArray[i], "");
  }
}

Upvotes: 4

Mark Giaconia
Mark Giaconia

Reputation: 3953

You might try this alternative non-recursive method (not that recursion is bad). Not sure if it's the right output but maybe give you some ideas.

public class CombineNames {

  public static void main(String[] args) {
    combine("von.del.smith.john.james");
  }

  private static void combine(String nameStr) {
    String[] tokens = nameStr.split("\\.");
    for (int i = 0; i < tokens.length; i++) {
      String parts="";
      for (int j = i + 1; j < tokens.length; j++) {
        parts+="."+tokens[j];
        System.out.println(tokens[i]  + parts);
      }

    }
  }

}

output:

von.del
von.del.smith
von.del.smith.john
von.del.smith.john.james
del.smith
del.smith.john
del.smith.john.james
smith.john
smith.john.james
john.james

Upvotes: 0

Related Questions