Technical Support (Codeforces 1754A)
Tags: Greedy, greedy

You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows.The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the total number of messages in the dialog.The second line of each test case consists of $$$n$$$ characters "Q" and "A", describing types of messages in the dialog in chronological order. Character "Q" denotes the message with client question, and character "A" — the message with technical support manager answer. It is guaranteed that the first character in the line equals to "Q".

Output: For each test case print "Yes" (without quotes) if dialog may correspond to the rules of work, or "No" (without quotes) otherwise.

Note: In the first test case the two questions from the client are followed with two specialist's answers. So this dialog may correspond to the rules of work.In the second test case one of the first two questions was not answered.In the third test case the technical support manager sent two messaged as the answer to the only message of the client.

Sample Inputs:
5
4
QQAA
4
QQAQ
3
QAA
1
Q
14
QAQQAQAAQQQAAA

Sample Outputs:
Yes
No
Yes
No
Yes

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    l = input()
    q = 0
    for i in l:
        if i == 'Q':
            q += 1
        elif q > 0:
            q -= 1
    if q == 0:
        print('Yes')
    else:
        print('No')

Solution (Python):
for _ in range(int(input())):
    (n, log) = (int(input()), input())
    questionBank = 0
    for x in range(n):
        if log[x] == 'Q':
            if questionBank < 0:
                questionBank = 0
            questionBank += 1
        else:
            questionBank -= 1
    if questionBank > 0:
        print('No')
    else:
        print('Yes')

Solution (Python):
t = int(input())
for i in range(t):
    n = int(input())
    x = input()
    ctn = 0
    for i in x:
        if i == 'Q':
            ctn += 1
        else:
            ctn -= 1
        if ctn < 0:
            ctn = 0
    if ctn == 0:
        print('Yes')
    else:
        print('No')