Check if strings in a list can be formed by concatenation of elements in the same list
For example:
Input List -
{ best, rockstar, star, guide, bestguide, rock }
Output :-
rockstar -> rock, star
bestguide -> best, guide
Here "rockstar" can be formed using rock and star. Similarly "bestguide" can be formed by joining "best" and "guide".
Solution so far I have- Create all the combinations of string by joining each other(2 string together, 3 string together and so on) and store in a Map.
map structure could be as following
Map<String, List<String>>
{rockstar : [rock, star], ....}
Now check just traverse original list and check in the map. If it's found then it's one of possible solution.
Looking for a better solution with better time/space complexity