String Building (Codeforces 1671A)
Tags: Implement, implementation

You are given a string $$$s$$$. You have to determine whether it is possible to build the string $$$s$$$ out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example:  aaaabbb can be built as aa $$$+$$$ aa $$$+$$$ bbb;  bbaaaaabbb can be built as bb $$$+$$$ aaa $$$+$$$ aa $$$+$$$ bbb;  aaaaaa can be built as aa $$$+$$$ aa $$$+$$$ aa;  abab cannot be built from aa, aaa, bb and/or bbb. 

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 50$$$), consisting of characters a and/or b.

Output: For each test case, print YES if it is possible to build the string $$$s$$$. Otherwise, print NO.You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

Note: The first four test cases of the example are described in the statement.

Sample Inputs:
8
aaaabbb
bbaaaaabbb
aaaaaa
abab
a
b
aaaab
bbaaa

Sample Outputs:
YES
YES
YES
NO
NO
NO
NO
YES

Solution (Python):
class Solution:

    def __init__(self):
        pass

    def solve(self):
        t = int(input())
        result = []
        for i in range(t):
            s = input()
            length = len(s)
            count = 1
            prev = s[0]
            poss = True
            if len(s) == 1:
                poss = False
            else:
                for char in s[1:]:
                    if char == prev:
                        count += 1
                    elif count == 1:
                        poss = False
                        break
                    else:
                        count = 1
                        prev = char
            if count == 1:
                poss = False
            if poss:
                result.append('YES')
            else:
                result.append('NO')
        for x in result:
            print(x)
if __name__ == '__main__':
    sol = Solution()
    sol.solve()

Solution (Python):
import sys, math

def get_intl():
    """"""
    return list(map(int, sys.stdin.readline().strip().split()))

def get_int():
    """"""
    return int(sys.stdin.readline().strip())

def get_strl():
    """"""
    return sys.stdin.readline().strip().split()

def get_str():
    """"""
    return sys.stdin.readline().strip()

def prt(msg=''):
    """"""
    if isinstance(msg, list):
        sys.stdout.write(' '.join(map(str, msg)) + '\n')
    else:
        sys.stdout.write(str(msg) + '\n')
sys.setrecursionlimit(100000000)
MOD = 998244353

def solve():
    inp = get_str()
    new = ' ' + inp + ' '
    if ' a ' in new.replace('b', ' ') or ' b ' in new.replace('a', ' '):
        prt('NO')
    else:
        prt('YES')

def main():
    haveTestCases = 1
    testCase = 1
    if haveTestCases:
        testCase = get_int()
    for i in range(testCase):
        solve()
main()

Solution (Python):
test = int(input())
for i in range(test):
    string = '_' + str(input()) + '_'
    if '_ab' in string:
        print('NO')
    elif '_ba' in string:
        print('NO')
    elif 'ab_' in string:
        print('NO')
    elif 'ba_' in string:
        print('NO')
    elif 'aba' in string:
        print('NO')
    elif 'bab' in string:
        print('NO')
    elif '_a_' in string:
        print('NO')
    elif '_b_' in string:
        print('NO')
    else:
        print('YES')