12. Balance Stars and Hashes
Difficulty: easy · String
Problem
You are given a string `S` made only of the characters `*` and `#`. A string is **valid** when it contains the same number of `*` and `#` characters (their positions do not matter). Report how far `S` is from being valid as a signed number: print (number of `*`) − (number of `#`). - If there are more `*`, the answer is positive. - If there are more `#`, the answer is negative. - If they are equal, the answer is `0`.
Input
A single line containing the string `S`.
Output
Print one integer: the count of `*` minus the count of `#`.
Example 1
Input: ###*** Output: 0
Explanation: Three '*' and three '#': 3 - 3 = 0.
Example 2
Input: **#* Output: 2
Explanation: Three '*' and one '#': 3 - 1 = 2, positive because '*' is in excess.
Example 3
Input: ##*## Output: -3
Explanation: One '*' and four '#': 1 - 4 = -3.
Constraints
- 1 <= |S| <= 10^5 - `S` contains only `*` and `#`
Solutions are judged against 3 sample and 10 hidden tests. Sign in to solve it · All problems