#C2243. Chemical Reaction Stability
Chemical Reaction Stability
Chemical Reaction Stability
You are given a string representing a sequence of chemical reactions. Each reaction is denoted by a character: 'A' increases the reaction balance by 1, and 'B' decreases it by 1. The material is considered Stable if, while processing the sequence from left to right, the balance never becomes negative and the final balance is exactly zero. Otherwise, it is Unstable.
You can mathematically express the balance after processing a reaction sequence \( s = s_1s_2\ldots s_n \) by the formula:
\[ \text{balance} = \sum_{i=1}^{n} \left( I(s_i = 'A') - I(s_i = 'B') \right), \]
where \( I(\cdot) \) is the indicator function. Additionally, for the sequence to be valid (Stable), it must hold that for every prefix \( s_1s_2\ldots s_k \) (for all \( 1 \le k \le n \)), the balance is non-negative, and the final balance is zero.
inputFormat
The input is read from stdin and consists of a single line containing a string reactions which is a sequence of characters that are either 'A' or 'B'. The string can be empty. There are no extra spaces or delimiters.
outputFormat
Output to stdout a single word: either Stable
if the material ends up stable under the given reactions, or Unstable
otherwise.
ABAB
Stable