0

I want to create words that come with a string in different ways as follows. I'm not sure how best to do this

input: Paul Thomas Anderson

output: Paul Thomas Anderson, P Thomas Anderson, T Anderson, Paul T Anderson, Paul Thomas A, T Anderson, Paul A, Pa Anderson ...

What would be the best and generic method to do this in java?

tobias_k
  • 81,265
  • 12
  • 120
  • 179
ilyo
  • 3
  • 1
  • 1
    1) Split string into words/name parts, 2) get i) initial, ii) first two letter, iii) full name for each part, 3) get Product of those sets. – tobias_k Sep 11 '20 at 16:48
  • 1
    See e.g. [here](https://stackoverflow.com/q/714108/1639625) for getting the product of all those sets. – tobias_k Sep 11 '20 at 18:03

1 Answers1

0

Ideally, you'd want to show what you've tried so far. That being said, the requirement you wrote is essentially as follows:

  • take a sentence and break it into each of its words
  • generate a tuple of answers made up of a combination of each word and/or the first k letters of each word.

In fact, when you write Paul Thomas Anderson, you're handling the special case where k = length(word).

Your answer is probably not going to be specific to Java and I think you might be better served in the software engineering Stack Exchange or the Programming Stack Exchange sites.

Start with something along the lines of:

List<List<String>>() result = new ArrayList<List<String>>();
String[] words = seed.split(" "); // this will give you each word
for (String word : words){
   for (int i = 1; i < word.length(); i++){
     String part = word.substring(0,i); // make sure length-1 is actually the right max
     // Do the saving here - you need a good structure - not sure my List of List cuts it
   }
}

Actually you should refer to this post for the cartesian product of your sets. This will simplify greatly what you need to do.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
  • Actually the more I think about it the more I think it needs recursion, not a simple for loop. You won't be able to get your structure right otherwise. – David Brossard Sep 11 '20 at 17:05