If you are looking for creating a single random word, then follow the next steps:
- Populate an array of chars:
char[] arr = { 'a', 'b', ... , 'z'}
- get a random integer that denotes the size of the string.
- initialize an empty string
s
- iterate from 0 to the drawed length, draw a number in range
[0,arr.length)
let it be x
, and append arr[x]
to the string s
.
If you are looking for all possibilities, you are looking for all combinations, and the simplest way to do it in my opinion is using recursion. The idea is to "guess" the first char, and run the recursion on the suffix of the string - repeat this for all first possibilities of first char, and you get all combinations.
Pseudo code:
getCombinations(set,idx,length,current):
if (idx == length):
set.add(copy(current))
return
for each char c:
current[idx] = c //setting the next char
getCombinations(set,idx+1,length,current) //invoking recursively on smaller range
invoke with getCombinations([],0,length,arr)
where []
is an empty set which will hold the results, length
is the length of the combinations generated, and arr
is an empty array.
To get combinations smaller then length
you can either add also substrings during the process or to invoke with smaller length
.
Note that the number of combinations is exponential in the length of the word, so it will consume a lot of time.