[LeetCode][1641. Count Sorted Vowel Strings] Image Explanation: Distributing N Balls into 5 Boxes, Some Boxes May Be Empty

By Long Luo

This article is the solution Image Explanation: Distributing N Balls into 5 Boxes, Some Boxes May Be Empty of Problem 1641. Count Sorted Vowel Strings.

Math

Every vowel string is start by a,e,i,o,ua, e, i, o, u, and there are NN strings.

How to distribute NN strings into the 55 boxes, some boxes may be empty?

Imaging that there are NN balls, we have to put them in to 55 boxes represented by the five vowels, as the picture shows.

1621

Once the number of characters in each box were fixed, the string is fixed.

Therefore, how many ways are there to put NN balls in 55 boxes, and the boxes can be empty?

The combinatorics explanation is here.

  1. NN balls in MM boxes, box can not empty: C(n1,m1)C(n - 1, m - 1).

  2. NN balls in MM boxes, box can be empty: C(n+m1,m1)C(n + m - 1, m - 1).

1
2
3
public static int countVowelStrings(int n) {
return (n + 4) * (n + 3) * (n + 2) * (n + 1) / 24;
}

Analysis

  • Time Complexity: O(1)O(1).
  • Space Complexity: O(1)O(1).

All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗