My brain isn't working, and this isn't really Permutation, for example, given the input :
ab
I want :
aa
ab
bb
ba
I'm not really sure where to start.
My brain isn't working, and this isn't really Permutation, for example, given the input :
ab
I want :
aa
ab
bb
ba
I'm not really sure where to start.
In Python, use itertools.product
:
>>> for xs in itertools.product('ab', repeat=2): # 2 == len('ab')
... print(xs)
...
('a', 'a')
('a', 'b')
('b', 'a')
('b', 'b')
>>> for xs in itertools.product('ab', repeat=2):
... print(''.join(xs))
...
aa
ab
ba
bb
In Javascript,
var myString = "ab", result = [];
for (var i = 0; i < myString.length; i += 1) {
for (var j = 0; j < myString.length; j += 1) {
result.push(myString[i] + myString[j]);
}
}
console.log(result);
Output
[ 'aa', 'ab', 'ba', 'bb' ]