Question regarding return in a function

Hello,

I've found the following code for counting how many instances of the word balloon can be made for a given text. However, I have difficulty in understanding the return, and specifically the use of min() function. WHile I can understand why some of the counts are divided by two (apparently because l and o are double within the word), I can understand what is the use of the min function here?

Thanks

 ``123456`` `````` int maxNumberOfBalloons(string text) { vector counts(128); for (char c : text) counts[c]++; return min(counts['o'] / 2, min(counts['l'] / 2, min(counts['n'], min(counts['b'], counts['a'])))); }``````
Where did you find the code ?
In leetcode. I am practicing in various problems and I looked at a solution to the counting balloon words number problem, and it kinda confused me the return part.
min(a, b) returns the lower of a or b. As used in the code, min() is nested so that b is itself a min() which has another min etc. Hence all the closing brackets at the end.

If there is say no 'a', then the minimum value for min(counts['b'], counts['a']) will be 0 as counts['a'] will be 0. This 0 will then propagate through the other min() and the result of the return will be 0.

If there is exactly 1 of n, b, a and 2 o and 2 l then the return value will be 1.

But consider balloonballoon

Then min(counts['b'], counts['a']) will be 2, which when propagated through the other min() will return the result 2 - as there are indeed 2 instances of the word balloon.

If there is say balloonbelloon

then the minimum value for min(counts['b'], ['a']) will be 1 as there is only 1 a. This is propagated through the other min() and the return value is 1 - there is only one instance of the word balloon.
Thank you seeplus! I couldn't understand the link between the number counting instances and the min function at the return. Everything's ok now. Thank you!
Topic archived. No new replies allowed.