Given a string:
String words = "Mary had a little lamb";
how to obtain a combination of sentence fragments while the order of occurrence of words in the original sentence is maintained ???
example:
{'Mary had a little lamb'}
{'Mary had a little', 'lamb'}
{'Mary had a', 'little lamb'}, {'Mary had a', 'little', 'lamb'}
{'Mary had', 'a little lamb'}, {'Mary had', 'a little', 'lamb'}, {'Mary had', 'a', 'little lamb'}, {'Mary had', 'a', 'little', 'lamb'}
{'Mary', 'had a little lamb'}, {'Mary', 'had a little', 'lamb'}, {'Mary', 'had a', 'little lamb'} and so on...
Thanks in advance 🙂
Think about it this way:
Each of these
<number>s can be either true or false. If it is true, then you cut the sentence in that location.So, if you have n+1 words, your problem gets reduced to going through binary representation of numbers with n bit, that is from 0 to 2^n-1
Examples: