FunctionRacerProblemsRace

Teams of a Given Size

EasyRecursionPython, JavaScript, TypeScript, Java, C++

names is a list of different strings. Return every possible team of exactly k of them. Each team is a list that keeps its members in the order they have in names, and the teams themselves may be returned in any order. When k is 0 the only team is the empty list, so return [[]]; when k is larger than the number of names there are no teams, so return [].

For example, names = ["ann", "bob", "cy"] and k = 2 give [["ann", "bob"], ["ann", "cy"], ["bob", "cy"]].

Example 1

names ["ann","bob","cy"]
k 2
returns [["ann","bob"],["ann","cy"],["bob","cy"]]

Three names give three ways to pick two of them, each kept in the original order.

Example 2

names ["ann","bob"]
k 0
returns [[]]

Picking nobody is the one and only team of size 0.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems