#B3946. 2014 Sequence
2014 Sequence
2014 Sequence
Given a string S consisting solely of digits from 0 to 9, determine whether it is possible to rearrange the characters of S via any number of swaps (including none) into a valid 2014 sequence.
A 2014 sequence is defined as a string obtained by concatenating one or more copies of the sequence 2014 together. For example, 20142014
and 201420142014
are valid 2014 sequences, whereas 2014201
and 572364
are not.
In other words, if we let k be a positive integer, a valid 2014 sequence is exactly 2014 repeated k times. Note that the digits in each copy appear in the order 2, 0, 1, 4.
Hint: Since any two digits can be swapped arbitrarily, you only need to check whether the frequency of the allowed digits in S is exactly what is needed to form a sequence of one or more copies of 2014. More formally, let n be the length of S. To form a valid 2014 sequence, n must be divisible by 4, and the counts of '2', '0', '1', and '4' must all equal n/4. Any other digit or frequency distribution will make it impossible.
inputFormat
The input consists of a single line containing the string S (with length between 1 and 1000) that is made up only of the characters '0'-'9'.
outputFormat
Output Yes
if it is possible to rearrange S into a valid 2014 sequence; otherwise, output No
.
sample
2014
Yes