At first I found this tricky, as the number of strings in the array is unknown, and it can be hard to iterate over unknown items of unknown length. Since repeated combinations are included, we know the total number of combinations (lengths of all strings multiplied). In my Ruby example below, I used strings "abcde", "123", and "YZ" . My method was to set up a resulting array of empty strings of length 30 (5*3*2). Then I examined the kind of combinations we will make: a1Y a1Z a2Y a2Z a3Y a3Z , b1Y etc.
Notice if we generate combinations in this order, a will repeat six times in a row, and this is because the remaining elements are three and two letters long (3*2). Similarly, the numbers in "123" will be repeated twice in a row, because the remaining (last) word has only two characters. This amount of repetition is assigned to variable *repeats* below. And each element of "YZ" repeats once in a row since there are no more elements after. If there is more room in the total combinations, then the word will repeat again from the beginning; e.g. for "123" the middle characters of the answers will be 1,1, 2,2,3,3,1,1,2,2,3,3 until 30 is reached. The strings of the res array are slowly built in this way. Ruby helps deal with some of the other minor issues. Pic below. icy` [image: Inline image 1] -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
<<image.png>>