Since this looks like homework I will try to explain solution but you are the one who needs to create code.
If we have elements like
a, b, c, d
tokens using 1 elements will be
a b c d
^ ^ ^ ^
tokens using 2 elements will be
a b c d
^^^
^^^
^^^
tokens using 3 elements will be
a b c d
^^^^^
^^^^^
and token using all 4 element will be
a b c d
^^^^^^^
So in case of each token of size n
we are iterating from a, b, and adding to it its next n-1
elements.
So method which will generate all tokens of length n
should work like this:
- for all elements from which we can create full tokens (if token size is 2 we can't use last element, if its size is 3 we can't use last 2 elements, ...)
- get next
n-1
elements and combine them in single token (collect that token in some collection so you can return it later)
Assuming that such method will be declared like List<String> generateTokens(String[] elements, int tokenSize)
for case
String[] elements = { "a", "b", "c", "d" };
we would need to invoke it 4 times to get
generateTokens(elements, 1) // ["a", "b", "c", "d"]
generateTokens(elements, 2) // ["a b", "b c", "c d"]
generateTokens(elements, 3) // ["a b c", "b c d"]
generateTokens(elements, 4) // ["a b c d"]
So as you see you need to invoke that method x
times (where x
is amount of elements) and gather in one place all results. You can easily achieve that with simple loop.