2
Randomly choose one out of k
-length, ordered subset of characters in a string, while only storing a limited number of characters. The subset must be chosen with equal probability and may contain repeated characters. Do this without generating all possible permutations and assume k is at most the length of the string. For example, the string daddy
has 7 subsets of length two: da
, dd
, dy
, ad
, ay
, yd
, ya
. The function should return any one of them with the probability of 1/7
.
1You probably don't mean substring.
te
isn't a substring for example. – Maltysen – 2017-03-11T20:03:12.6301It would be nice to have a bit of a stronger specification. An example is not really a satisfactory specification. – Post Rock Garf Hunter – 2017-03-11T20:03:40.053
3The usual term for non-contiguous substrings is "subsequence". But since you also seem to allow arbitrary orders, you probably want "subsets" or "sub-multisets", since you've included
ee
. – Martin Ender – 2017-03-11T20:06:53.937updated to subsets – ash – 2017-03-13T01:46:12.153
1I think this question is asking, "choose 1 out of a set of k-lengthed, ordered subsets of the characters in a word, where the same character is equivalent (e.g. 'te' with the first e = 'te' with the second), with equal prabability" – MildlyMilquetoast – 2017-03-13T01:50:00.283
If I were to change this challenge, I would add more test cases, separate from the explanation of what to do (down below, probably). Also, I would remove the restriction on how the program/function finds an ordered subset, I don't see a clear reason of why it's there. Also, I might remove the "uniformly random" part to make it just "random", but that is up to you. You might want to be more clear on what exactly is the input and output for the program, and what it should do if k is greater than the string length, or if k <= 0. Look at some other questions (with decent scores) for guidance. – MildlyMilquetoast – 2017-03-13T02:06:55.503