Find Duplicate Permutations

Problem

There are marbles numbered from 1 to N. Of these, duplicates are allowed, and all methods of selecting M times and arranging them are printed out.

input

The first line is given the natural numbers N(3<=N<=10) and M(2<=M<=N).

3 2

output

  • Prints the result on the first line. Prints the last total number of cases.
  • The output order is alphabetically in ascending order.
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
9

Solution

for loop(wrong answer) - need to add more loop following m

function solution(n, m) {
	let answer = [];

	for (let i = 1; i <= n; i++) {
		for (let j = 1; j <= n; j++) {
			console.log(i, j);
		}
	}

	return answer;
}

recursive - it can be increased by m

function solution(n, m) {
	let answer = [];
	let tmp = Array.from({ length: m }, () => 0);

	function DFS(L) {
		if (L >= m) {
			answer.push([...tmp]);
		} else {
			for (let i = 1; i <= n; i++) {
				tmp[L] = i;
				DFS(L + 1);
			}
		}
	}

	DFS(0);

	return answer;
}