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

s = input()
qc = s.count('Q')
qs = int(qc ** 0.5)
hc = s.count('H')
if qs == 0:
    print('YES')
    sys.exit(0)
if not qc == qs ** 2:
    print('NO')
    sys.exit(0)
if not hc % (qs + 1) == 0:
    print('NO')
    sys.exit(0)

t = s.split('Q')
pre = len(t[0]) // 2
suf = 0 if len(t) == 1 else len(t[-1]) // 2
a = ['H' * pre] + t[1 : qs] + ['H' * suf]
o = 'Q'.join(a)
g = ''
for c in o:
    if c == 'H':
        g += 'H'
    else:
        g += o

print('YES' if g == s else 'NO')
