Skip to content
Advertisement

breaking down words in different ways in java

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement