You should get string of chemical equation (no spaces, only letters (upper-case and lower-case), numbers, brackets and math signs) from user and print the answer if equation is balanced or not (any pair of positive/negative answers: Yes/No, true/false, 1/0). To make code shorter you can assume that input strings can contain only these elements: Al, Ar, B, Be, C, Cl, Cr, Cu, Fe, H, He, K, N, O, S. And one more thing: there could be -
signs. It's all about math: +
means addition, -
means subtraction.
Examples:
Input:
C6H5COOH-O2=7CO2+3H2O
Output:
No
Input:
2Fe(CN)6+2SO2+202=Fe2(SO4)2+6C2N2
Output:
Yes
Input:
2SO2=2SO4-2O2
Output:
Yes
The shortest code wins.
2O2
instead of202
(two hundred and two). \$\endgroup\$