The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ...
The only line of the input is a string between 1 and 10<sup class="upper-index">6</sup> characters long.
import sys
import math

def yes():
  print('YES')
  sys.exit(0)
def no():
  print('NO')
  sys.exit(0)

def out(prog):
  res = []
  for c in prog:
    if c == 'H':
      res.append(c)
    else:
      res.extend(prog)
  return ''.join(res)

s = input()
if s.count('Q') == 0:
  yes()

h = s.count('H')
q = s.count('Q')

true_q = 0
rq = int(math.sqrt(q))
for i in range(rq - 2, rq + 3):
  if i * i == q:
    true_q = i
    break
else:
  no()

# h = th * (Q + 1)
true_h = h // (true_q + 1)
if h % (true_q + 1):
  no()

cnt_pref = 0
for i in range(len(s)):
  if s[i] == 'Q':
    if cnt_pref % 2:
      no()
    break
  cnt_pref += 1

cnt_suf = 0
for i in range(len(s) - 1, -1, -1):
  if s[i] == 'Q':
    if cnt_suf % 2:
      no()
    break
  cnt_suf += 1

cnt_pref //= 2
cnt_suf //= 2

prog = ''
cnt = 0
for i in range(len(s)):
  if s[i] == 'Q':
    cnt += 1
  if 1 <= cnt <= true_q:
    prog += s[i]
  if cnt == true_q:
    break

prog = 'H' * cnt_pref + prog + 'H' * cnt_suf

if out(prog) == s:
  yes()
no()