1.2 All Subsets of Size K

Print all subsets of size k from an array of size n

Assumption: No duplicate

Solution:

  1. Similar to Q1.1, only have to make sure the branches stop when subset size is k and does not go all the way down.

  2. Stop condition: when subset size reaches k and all letters have been considered.

//base case

if (sb.length() == k) {

res.add(sb.toString());

return;

}

if (index == array.length) {

return;

}

Last updated

Was this helpful?