-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMagicNumbers.java
45 lines (43 loc) · 909 Bytes
/
MagicNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*https://codeforces.com/problemset/problem/320/A*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MagicNumbers
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();
boolean result = true;
int oneCounter = 0, fourCounter = 0;
for (int i = 0; i < str.length(); ++i)
{
if (str.charAt(i) == '1')
{
fourCounter = 0;
++oneCounter;
}
else if (str.charAt(i) == '4')
{
if (oneCounter > 0) oneCounter = 0;
else if (oneCounter == 0 && fourCounter == 0)
{
result = false;
break;
}
if (fourCounter == 2)
{
result = false;
break;
}
++fourCounter;
}
else
{
result = false;
break;
}
}
System.out.println(result ? "YES" : "NO");
}
}