'Java or pseudocode for creating text combination

I am trying to break a string into combinations as following:

If input = ab
Result of all combinations:
ab
a b

If input = abc
Result of all combinations:
abc 
a bc
ab c
a b c

If input = abcd
Reslut of all combinations:
abcd
a bc d
a bcd
a b cd
a b c d
ab cd
ab c d
abc d

If input = abcde
Reslut of alla combinations:
abcde
a bcde
a b cde
a b c de
a b cd e
a b c d e
a bc de
a bc d e
a bcd e
ab cde
ab cd e
ab c de
ab c d e
abc de
abc d e
abcd e

This will be very hard manually if the string is as long as abcdefghijklm. Do you have any idea how I can do this programmatically?



Solution 1:[1]

The binary counting approach from the link will work, but instead you'll use the 1s and 0s to determine if a space should be present or not between letters.

We count in decimal from zero up to 2^(numLetters - 1) - 1. When we convert the decimal number to binary, we left pad using zeroes for a total length of numLetters - 1.

For example, here's the "correct order" that I'd expect to get the combinations in for abcde:

enter image description here

Would generating the combinations in this order be acceptable? If so, I'll post some example code to accomplish this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idle_Mind