#K33272. Rearrange Digits to Validate Expression
Rearrange Digits to Validate Expression
Rearrange Digits to Validate Expression
You are given a mathematical expression in the format LHS=RHS where both sides may contain digits and the operators + and -. The challenge is to determine whether it is possible to rearrange the digits (while keeping the operators and the locations of non‐digit characters fixed) such that the resulting expression becomes a valid equality. More formally, if we denote the original expression as
\( \texttt{expr} = E_1 = E_2 \),
you must check if there exists a permutation of all digit characters in \(E_1\) and \(E_2\) that can be reinserted in place of the original digits so that
\( \texttt{eval}(E_1') = \texttt{eval}(E_2') \)
where \(E_1'\) and \(E_2'\) are the expressions after substitution and \(\texttt{eval}(\cdot)\) represents the evaluation of the arithmetic expression. Output YES if such a rearrangement exists, otherwise output NO.
Note: The expressions will only contain non-negative integers, plus ('+') and minus ('-') operators, and exactly one '=' symbol.
inputFormat
The input consists of a single line containing the expression string. There are no extra spaces in the input.
Example:
2+2=4
outputFormat
For the given expression, output a single line: YES if a valid rearrangement exists that makes the equality true, or NO otherwise.
Example:
YES## sample
2+2=4
YES