Solution using itertools