A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.

You're given a number. Determine if it is a magic number or not.


-----Input-----

The first line of input contains an integer n, (1 ≤ n ≤ 10^9). This number doesn't contain leading zeros.


-----Output-----

Print "YES" if n is a magic number or print "NO" if it's not.


-----Examples-----
Input
114114

Output
YES

Input
1111

Output
YES

Input
441231

Output
NO

Difficulty: interview

Starter code:

Solutions:
n = input()
good = True
while n != '' and good:
    if n.endswith('144'):
        n = n[:-3]
    elif n.endswith('14'):
        n = n[:-2]
    elif n.endswith('1'):
        n = n[:-1]
    else:
        good = False
print('YES' if good else 'NO')


#!/usr/bin/env python3
t = input()

def is_magic(s):
  if (len(s) >= 3):
    if s[-3:] == '144':
      return is_magic(s[:-3])
  if (len(s) >= 2):
    if s[-2:] == '14':
      return is_magic(s[:-2])
  if (len(s) >= 1):
    if s[-1:] == '1':
      return is_magic(s[:-1])
  if (len(s) == 0):
    return True
  return False

if is_magic(t):
  print("YES")
else:
  print("NO")


def main():
  s = input()
  
  if s.count('444') > 0:
    print('NO')
    return()
  s = s.replace('144','')
  s = s.replace('14','')
  s = s.replace('1','')
  if len(s) == 0:
    print('YES')
  else:print('NO')
  
main()

s = input().rstrip()
good  = '14'
for j in s:
    if j not in good:
        print('NO')
        return
cur = ''
for  i in range(len(s)):
    if s[i]=='1':
        cur='1'
    else:
        cur+='4'
        if cur=='14' and i!=len(s)-1 and s[i+1]=='4':
            continue
        if cur!='14' and cur !='144':
            print('NO')
            return
        else:
            cur=''
print('YES')


x = str(input())
i=0
while i <= (len(x)-1):
    if x[i] == '1':
        if x[i:i+2] == '14':
            if x[i:i+3] == '144':
                i += 3
            else: i += 2
        else: i += 1
    else:
        print('NO')
        break
else: print('YES')


def main():
    x=str(input())
    xix=int(x)
    xs=list(set(list(x)));
    if len(xs)==1 and xs[0]=='1':
        print ("YES")
    elif x[0]=="4":
        print ("NO")
    elif len(xs)==2:
        xs.sort()
        ncs="".join(xs)
        if ncs=="14":
            if len(x)>=3 and x[0:3]=="441":
                print ("NO")
            else:
                ind1=x.find("444")
                if ind1>=0 or x=="414":
                    print ("NO")
                else:
                    print ("YES")
        else:
            print ("NO")
    else:
        print ("NO")
    
main()

def main():
    s=str(input())
    ss=set(s)
    if (s=='1'):
        print("YES")
        return
    if (s=='14'):
        print("YES")
        return
    if (s=='144'):
        print("YES")
        return
    if (s.find('444')!=-1):
        print('NO')
        return 
    
    f1=(s.find('1')!=-1 or s.find('14')!=-1 or s.find('14')!=-1)
    f2=(ss=={'1', '4'} or ss=={'1'})
    f3= (s.count('1')>1)
    f4= (s[0]=='1')
    if (f1 and f2 and f3 and f4):
        print("YES")
    else:
        print("NO")
   
   
main()


def main():
    s=str(input())
    if (s=='1'):
        print("YES")
        return
    if (s=='14'):
        print("YES")
        return
    if (s=='144'):
        print("YES")
        return
    if (s.find('444')!=-1):
        print('NO')
        return
    
    f2=(s.count('4')+ s.count('1'))==len(s)
    f3= (s.count('1')>1)
    f4= (s[0]=='1')
    if (f2 and f3 and f4):
        print("YES")
    else:
        print("NO")
 
main()


import sys
str = input()

for i in range(0, len(str)):
    if str[i] == '1':
        continue
    elif str[i] == '4':
        if i > 0 and str[i - 1] == '1':
            continue
        elif i > 1 and str[i - 1] == '4' and str[i - 2] == '1':
            continue
        else:
            print('NO')
            return
    else:
        print('NO')
        return
        
print('YES')


s=input()
n=len(s)
k=True
for item in s:
    if(item in "02356789"):
        print("NO")
        k=False
        break
if(k):
    s=s.replace("144","0")
    s=s.replace("14","0")
    s=s.replace("1","0")
    if(s.count("0")==len(s)):
        print("YES")
    else:
        print("NO")
    


import sys

n = input()
for i in range(len(n)):
	if n[i] != '1' and n[i] != '4':
		print("NO")
		return
while len(n) > 0:
	s = n[0:3]
	if s == "144":
		n = n[3:]
	elif s[0:2] == "14":
		n = n[2:]
	elif s[0] == "1":
		n = n[1:]
	else:
		print("NO")
		return
print("YES")


ans = True
s = 2
for c in input().strip():
    if c == '1':
        s = 0
    elif c == '4':
        s += 1
        if s >= 3:
            ans = False
    else:
        ans = False

print("YES" if ans else "NO")


I=input()
count=0
four=0
for i in I:
    if i!="1":
        if i!="4":
            print("NO")
            break
        else:
            if four==0:
                print("NO")
                break
            else:
                count+=1
                four-=1
    else:
        count+=1
        four=2
if count==len(I): print("YES")

def solve(num):
    return num[0] == '1' and all(x == '' or x == '4' or x == '44' for x in num.split('1'))

def __starting_point():
    num = input()
    is_magical = solve(num)
    print('YES' if is_magical else 'NO')

__starting_point()

def check(s, dep):
	if len(s) == 0:
		return True;
	flag = False
	if s[-1:] == '1':
		flag = flag or check(s[:-1], dep + 1)
	if s[-2:] == '14':
		flag = flag or check(s[:-2], dep + 1)
	if s[-3:] == '144':
		flag = flag or check(s[:-3], dep + 1)
	return flag


s = input()
if check(s, 0):
	print('YES')
else:
	print('NO')

3

def isgood(n):
    if n == '1' or n == '14' or n == '144':
        return True
    if n.startswith('144'):
        return isgood(n[3:])
    if n.startswith('14'):
        return isgood(n[2:])
    if n.startswith('1'):
        return isgood(n[1:])
    return False

n = input()
if isgood(n):
    print('YES')
else:
    print('NO')


s = (input())

s2 = ""
p = -1
s+="11"
for i in range(len(s)-2):
    if (s[i] != '1' or s[i+1] != '4' or s[i+2] != '4') and (i>p):
        s2+= s[i]
    else:
        if p<i:
            p = i+2
            s2+='*'
p=-1;
s = s2
s2 = ""
s+="11"
for i in range(len(s)-1):
    if (s[i] != '1' or s[i + 1] != '4') and (i>p):
        s2+= s[i]
    else:
        if p<i:
            p = i+1
            s2+='*'
p=-1;
s = s2
s2 = ""

for i in range(len(s)):
    if s[i] != '1':
        s2+=s[i]
s = s2
for i in range(len(s)):
    if s2[i]!='*':
        print("NO")
        return
print("YES")

import re
line = input()
match = re.match("([1][4]{0,2})+",line)
if match and match.group(0) == line:
    print("YES")
else:
    print("NO")

s = input()
ss = "144"
i, j = 0, 0
n = len(s)
vale = True
while vale and i < n:
  if j == 3 or s[i] != ss[j]:
    j = 0
  if s[i] != ss[j]:
    vale = False
  j += 1
  i += 1

print("YES" if vale else "NO")


from re import match

print(match(r"^(14?4?)*$", input()) and 'YES' or 'NO')

n = input()
c = 2
s = 'YES'
for i in n:
  if i != '1' and i != '4':
    s = 'NO'
    break
  elif i == '4':
    c += 1
    if c > 2: 
      s = 'NO'
      break
  else: c = 0
print (s)


number = input()
l = len(number)

if number[0] != '1':
    print('NO')
elif number.count('1')+number.count('4') != l:
    print('NO')
elif number.count('444') != 0:
    print('NO')
else:
    print('YES')

s = input()
print('YES' if s[0] == '1' and all(q == '' or q == '4' or q == '44' for q in s.split('1')) else 'NO')

v = input()
i = 0
while i <= len(v) - 1:
    if v[0] != "1":
        print("NO")
        break
    if v[i] == "1":
        if i + 1 <= len(v) - 1:
            if v[i + 1] == "4":
                if i + 2 <= len(v) - 1:
                    if v[i + 2] == "4":
                        i += 2
                    else:
                        i += 1
                else:
                    print('YES')
                    break
    else:
        print("NO")
        break
    i += 1
else:
    print('YES')




v = input()
i = 0
while i <= len(v) - 1:
    # if v[0] != "1":
    #     print("NO")
    #     break
    if v[i] == "1":
        if i + 1 <= len(v) - 1:
            if v[i + 1] == "4":
                if i + 2 <= len(v) - 1:
                    if v[i + 2] == "4":
                        i += 2
                    else:
                        i += 1
                else:
                    print('YES')
                    break
    else:
        print("NO")
        break
    i += 1
else:
    print('YES')