Problem:
 Score : 600 points    Problem Statement Let us consider the following operations on a string consisting of A and B :   Select a character in a string. If it is A , replace it with BB . If it is B , replace with AA .  Select a substring that is equal to either AAA or BBB , and delete it from the string.   For example, if the first operation is performed on ABA and the first character is selected, the string becomes BBBA .
If the second operation is performed on BBBAAAA and the fourth through sixth characters are selected, the string becomes BBBA .  These operations can be performed any number of times, in any order.  You are given two string S and T , and q queries a_i, b_i, c_i, d_i .
For each query, determine whether S_{a_i} S_{{a_i}+1} ... S_{b_i} , a substring of S , can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i} , a substring of T .      Constraints  1 \leq |S|, |T| \leq 10^5  S and T consist of letters A and B .  1 \leq q \leq 10^5  1 \leq a_i \leq b_i \leq |S|  1 \leq c_i \leq d_i \leq |T|         Input Input is given from Standard Input in the following format:  S  T  q  a_1  b_1  c_1  d_1  ...  a_q  b_q  c_q  d_q       Output Print q lines. The i -th line should contain the response to the i -th query. If S_{a_i} S_{{a_i}+1} ... S_{b_i} can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i} , print YES . Otherwise, print NO .        Sample Input 1 BBBAAAABA
BBBBA
4
7 9 2 5
7 9 1 4
1 7 2 5
1 7 2 4      Sample Output 1 YES
NO
YES
NO  The first query asks whether the string ABA can be made into BBBA .
As explained in the problem statement, it can be done by the first operation.  The second query asks whether ABA can be made into BBBB , and the fourth query asks whether BBBAAAA can be made into BBB .
Neither is possible.  The third query asks whether the string BBBAAAA can be made into BBBA .
As explained in the problem statement, it can be done by the second operation.       Sample Input 2 AAAAABBBBAAABBBBAAAA
BBBBAAABBBBBBAAAAABB
10
2 15 2 13
2 13 6 16
1 13 2 20
4 20 3 20
1 18 9 19
2 14 1 11
3 20 3 15
6 16 1 17
4 18 8 20
7 20 3 14      Sample Output 2 YES
YES
YES
YES
YES
YES
NO
NO
NO
NO

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

S = input()
T = input()
typeS = [0 for _ in range(1+len(S))]
typeT = [0 for _ in range(1+len(T))]
typeS[0] = typeT[0] = 0 
for i in range(len(S)):
    ctr = 2
    if S[i] == 'A':
        ctr = 1
    typeS[i+1] = (typeS[i] + ctr)%3
for i in range(len(T)):
    ctr = 2
    if T[i] == 'A':
        ctr = 1
    typeT[i+1] = (typeT[i] + ctr)%3
Q = int(input())
for _ in range(Q):
    a, b, c, d = inpl()
    if (typeS[b] - typeS[a-1])%3 == (typeT[d] -typeT[c-1])%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
s = input()
t = input()
q = int(input())
sl,tl = len(s),len(t)
sa,sb,ta,tb = [0]*(sl+1),[0]*(sl+1),[0]*(tl+1),[0]*(tl+1)
for i in range(sl):
	if s[i]=="A": sa[i+1],sb[i+1] = sa[i]+1,sb[i]
	else: sa[i+1],sb[i+1] = sa[i],sb[i]+1
for i in range(tl):
	if t[i]=="A": ta[i+1],tb[i+1] = ta[i]+1,tb[i]
	else: ta[i+1],tb[i+1] = ta[i],tb[i]+1
for _ in range(q):
	a,b,c,d = (int(i) for i in input().split())
	if (sa[b]-sa[a-1]-sb[b]+sb[a-1]-ta[d]+ta[c-1]+tb[d]-tb[c-1])%3: print("NO")
	else: print("YES")

Solution (Python):
S = input()
T = input()

ms = [0]
for s in S:
    if s == 'A':
        ms.append(ms[-1] + 1)
    else:
        ms.append(ms[-1] - 1)

mt = [0]
for t in T:
    if t == 'A':
        mt.append(mt[-1] + 1)
    else:
        mt.append(mt[-1] - 1)

q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    if (ms[b] - ms[a - 1]) % 3 == (mt[d] - mt[c - 1]) % 3:
        print('YES')
    else:
        print('NO')

Solution (Python):
S = input()
T = input()
def sign(s):
    if len(s)==1:
        return s
    elif s=="AA":
        return "B"
    elif s in ["AB", "BA"]:
        return ""
    elif s=="BB":
        return "A"

def diff(a, b):
    if b=="":
        return a
    elif b==a:
        return ""
    elif a=="":
        if b=="A":
            return "B"
        elif b=="B":
            return "A"
    else:
        return b

a = ""
Sn = [""]
for s in S:
    a = sign(a+s)
    Sn.append(a)
a = ""
Tn = [""]
for t in T:
    a = sign(a+t)
    Tn.append(a)

q = int(input())
for i in range(q):
    a, b, c, d = map(int, input().split())
    if diff(Sn[b], Sn[a-1]) == diff(Tn[d], Tn[c-1]):
        print("YES")
    else:
        print("NO")

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, T, ABCD):

    S_ = [0]
    for c in S:
        S_.append((1 if c == 'A' else 2) + S_[-1])
    T_ = [0]
    for c in T:
        T_.append((1 if c == 'A' else 2) + T_[-1])

    for a, b, c, d in ABCD:
        s = (S_[b]-S_[a-1]) % 3
        t = (T_[d]-T_[c-1]) % 3
        if s == t:
            print('YES')
        else:
            print('NO')


def main():
    S = read_str()
    T = read_str()
    q = read_int()
    ABCD = [read_int_n() for _ in range(q)]
    slv(S, T, ABCD)


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, T, ABCD):

    S_ = [0]
    for c in S:
        S_.append((1 if c == 'A' else 2) + S_[-1])
    T_ = [0]
    for c in T:
        T_.append((1 if c == 'A' else 2) + T_[-1])

    for a, b, c, d in ABCD:
        s = (S_[b]-S_[a-1]) % 3
        t = (T_[d]-T_[c-1]) % 3
        if s == t:
            print('YES')
        else:
            print('NO')


def main():
    S = read_str()
    T = read_str()
    q = read_int()
    ABCD = [read_int_n() for _ in range(q)]
    slv(S, T, ABCD)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
T = input()
S_a, S_b = [0], [0]
T_a, T_b = [0], [0]

for s in S:
    if s == "A":
        S_a.append(S_a[-1] + 1)
        S_b.append(S_b[-1])
    if s == "B":
        S_a.append(S_a[-1])
        S_b.append(S_b[-1] + 1)

for t in T:
    if t == "A":
        T_a.append(T_a[-1] + 1)
        T_b.append(T_b[-1])
    if t == "B":
        T_a.append(T_a[-1])
        T_b.append(T_b[-1] + 1)


def calc(na, nb):
    if na > nb:
        diff = na- nb
        if diff % 3 == 0:
            return ""
        elif (diff % 3) % 2 == 0:
            return "B"
        else:
            return "A"
    else:
        diff = nb - na
        if diff % 3 == 0:
            return ""
        elif (diff % 3) % 2 == 0:
            return "A"
        else:
            return "B"


Q = int(input())
for q in range(Q):
    a, b, c, d = map(int, input().split())
    sa = S_a[b] - S_a[a - 1]
    sb = S_b[b] - S_b[a - 1]
    ta = T_a[d] - T_a[c - 1]
    tb = T_b[d] - T_b[c - 1]

    SQ = calc(sa, sb)
    TQ = calc(ta, tb)

    print("YES" if SQ == TQ else "NO")

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
T = input()
q = int(input())

tmp = 0
S_SUM = [0]
for s in S:
	if s == 'A':
		tmp += 2
	else:
		tmp += 1
	S_SUM.append(tmp)

T_SUM = [0]
tmp = 0
for t in T:
	if t == 'A':
		tmp += 2
	else:
		tmp += 1
	T_SUM.append(tmp)

for i in range(q):
	a,b,c,d = inpl()
	sa = (S_SUM[b]-S_SUM[a-1]) - (T_SUM[d]-T_SUM[c-1])
	if sa%3 == 0:
		print('YES')
	else:
		print('NO')

Solution (Python):
s = input()
t = input()
q = int(input())

sa_pre = [s[i] == "A" for i in range(len(s))]
ta_pre = [t[i] == "A" for i in range(len(t))]

sa = [0, sa_pre[0]]
ta = [0, ta_pre[0]]

for i in range(1, len(s)):
	sa.append(sa[-1] + sa_pre[i])

for i in range(1, len(t)):
	ta.append(ta[-1] + ta_pre[i])

def check(a, b, c, d):
	xa = sa[b] - sa[a - 1]
	ya = ta[d] - ta[c - 1]
	if (2 * xa - (b - a + 1)) % 3 == (2 * ya - (d - c + 1)) % 3:
		print("YES")
		return
	else:
		print("NO")
		return

for _ in range(q):
	a, b, c, d = map(int, input().split())
	check(a, b, c, d)

Solution (Python):
s = input()
t = input()
q = int(input())

sa_pre = [s[i] == "A" for i in range(len(s))]
ta_pre = [t[i] == "A" for i in range(len(t))]

sa = [0, sa_pre[0]]
ta = [0, ta_pre[0]]

for i in range(1, len(s)):
	sa.append(sa[-1] + sa_pre[i])

for i in range(1, len(t)):
	ta.append(ta[-1] + ta_pre[i])

def check(a, b, c, d):
	xa = sa[b] - sa[a - 1]
	ya = ta[d] - ta[c - 1]
	if (2 * xa - (b - a + 1)) % 3 == (2 * ya - (d - c + 1)) % 3:
		print("YES")
		return
	else:
		print("NO")
		return

for _ in range(q):
	a, b, c, d = map(int, input().split())
	check(a, b, c, d)

Solution (Python):
S=input()
T=input()
N=len(S)
M=len(T)
U=[0 for i in range(N+1)]
V=[0 for i in range(M+1)]
for i in range(N):
    if S[i]=="A":
        U[i+1]=(U[i]+1)%3
    else:
        U[i+1]=(U[i]-1)%3
for i in range(M):
    if T[i]=="A":
        V[i+1]=(V[i]+1)%3
    else:
        V[i+1]=(V[i]-1)%3
q=int(input())
O=["" for i in range(q)]
for inp in range(q):
    a,b,c,d=map(int,input().split())
    if (U[b]-U[a-1]-V[d]+V[c-1])%3==0:
        O[inp]="YES"
    else:
        O[inp]="NO"
for i in O:
    print(i)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import bisect
from heapq import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

def enc(s):
  return [(1 if c == 'A' else 2) for c in s]

S = enc(input())
T = enc(input())

psumS = [0] + list(accumulate(S))
psumT = [0] + list(accumulate(T))

q = read()
for _ in range(q):
  a, b, c, d = reads()
  ms = psumS[b] - psumS[a-1]
  mt = psumT[d] - psumT[c-1]
  ans = (ms - mt) % 3 == 0
  print("YES" if ans else "NO")

Solution (Python):
S,T=[0],[0]
for c in input():S+=[S[-1]+1+(c=='B')]
for c in input():T+=[T[-1]+1+(c=='B')]
for _ in[0]*int(input()):
    a,b,c,d=map(int,input().split())
    print('NO'if(T[d]-T[c-1]-S[b]+S[a-1])%3 else'YES')

Solution (Python):
S,T=[0],[0]
for k,c in enumerate(input()):S+=[S[-1]+1+(c=='B')]
for k,c in enumerate(input()):T+=[T[-1]+1+(c=='B')]
for _ in[0]*int(input()):
    a,b,c,d=map(int,input().split())
    print('NO'if(T[d]-T[c-1]-S[b]+S[a-1])%3 else'YES')

Solution (Python):
S,T=[0],[0]
for k,c in enumerate(input()):S+=[S[-1]+1+(c=='B')]
for k,c in enumerate(input()):T+=[T[-1]+1+(c=='B')]
for _ in[0]*int(input()):
    a,b,c,d=map(int,input().split())
    print(['YES','NO'][(T[d]-T[c-1]-S[b]+S[a-1])%3>0])

Solution (Python):
S,T=[0],[0]
for k,c in enumerate(input()):S+=[S[-1]+(c=='A')]
for k,c in enumerate(input()):T+=[T[-1]+(c=='A')]
for _ in[0]*int(input()):
    a,b,c,d=map(int,input().split())
    print(['YES','NO'][(2*(b-a-d+c)+T[d]-T[c-1]-S[b]+S[a-1])%3>0])

Solution (Python):
S = input()
T = input()
Q = int(input())
qs = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(Q)]

smem = [0]
for c in S:
    if c == 'A':
        smem.append(smem[-1] + 1)
    else:
        smem.append(smem[-1] - 1)

tmem = [0]
for c in T:
    if c == 'A':
        tmem.append(tmem[-1] + 1)
    else:
        tmem.append(tmem[-1] - 1)

anss = []
for s1,s2,t1,t2 in qs:
    a = smem[s2+1] - smem[s1]
    b = tmem[t2+1] - tmem[t1]
    if a%3 == b%3:
        anss.append('YES')
    else:
        anss.append('NO')
print(*anss, sep='\n')

Solution (Python):
S = input()
T = input()
def count(S):
    L = len(S)
    cA = [0]*(L+1)
    cB = [0]*(L+1)
    for i in range(L):
        cA[i+1] = cA[i] + (S[i] == 'A')
        cB[i+1] = cB[i] + (S[i] == 'B')
    return cA, cB

sA, sB = count(S)
tA, tB = count(T)
q = int(input())
ans = []
for i in range(q):
    a, b, c, d = map(int, input().split())
    dA = (sA[b] - sA[a-1]) - (tA[d] - tA[c-1])
    dB = (sB[b] - sB[a-1]) - (tB[d] - tB[c-1])
    if (2*dA + dB) % 3 == 0 and (dA + 2*dB) % 3 == 0:
        ans.append('YES')
    else:
        ans.append('NO')
print(*ans, sep='\n')

Solution (Python):
S = input()
T = input()

cumsa = [0]
cumsb = [0]
for c in S:
    cumsa.append(cumsa[-1] + int(c == 'A'))
    cumsb.append(cumsb[-1] + int(c == 'B'))
cumta = [0]
cumtb = [0]
for c in T:
    cumta.append(cumta[-1] + int(c == 'A'))
    cumtb.append(cumtb[-1] + int(c == 'B'))

Q = int(input())
ans = []
for i in range(Q):
    a,b,c,d = map(int,input().split())
    sa = cumsa[b] - cumsa[a-1]
    sb = cumsb[b] - cumsb[a-1]
    ta = cumta[d] - cumta[c-1]
    tb = cumtb[d] - cumtb[c-1]
    ans.append('YES' if (sa-sb)%3 == (ta-tb)%3 else 'NO')

print('\n'.join(ans))

Solution (Python):
ds = [0]
tmp = 0
for c in input():
  tmp += [-1, 1][c == 'A']
  ds.append(tmp)

dt = [0]
tmp = 0
for c in input():
  tmp += [-1, 1][c == 'A']
  dt.append(tmp)

q = int(input())
for i in range(q):
  a, b, c, d = map(int, input().split())
  if abs((ds[b] - ds[a-1]) - (dt[d] - dt[c-1])) % 3 == 0:
    print('YES')
  else:
    print('NO')

Solution (Python):
Ss = input()
Ts = input()
q = int(input())
querys = [tuple(map(lambda x: int(x) - 1, input().split())) for i in range(q)]

def countA(Cs):
    numAs = [0] * (len(Cs) + 1)

    for i, C in enumerate(Cs, 1):
        if C == 'A':
            numAs[i] = numAs[i - 1] + 1
        else:
            numAs[i] = numAs[i - 1]

    return numAs


numASs = countA(Ss)
numATs = countA(Ts)
        
for a, b, c, d in querys:
    lenS = b - a + 1
    numAS = numASs[b + 1] - numASs[a]
    lenT = d - c + 1
    numAT = numATs[d + 1] - numATs[c]

    if ((numAS - (lenS - numAS)) - (numAT - (lenT - numAT))) % 3 == 0:
        print('YES')
    else:
        print('NO')

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    S = input()
    T = input()

    s_sum, t_sum = [0] * (len(S) + 1), [0] * (len(T) + 1)
    for i in range(len(S)):
        s_sum[i + 1] = s_sum[i] + (1 if S[i] == "A" else 2)

    for i in range(len(T)):
        t_sum[i + 1] = t_sum[i] + (1 if T[i] == "A" else 2)

    for _ in range(int(input())):
        a, b, c, d = map(lambda x: int(x) - 1, input().split())
        s = s_sum[b + 1] - s_sum[a]
        t = t_sum[d + 1] - t_sum[c]
        print("YES" if s % 3 == t % 3 else "NO")


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
T = input()
q = int(input())
A = [list(map(int, input().split())) for _ in range(q)]

count_s = [{"A": 0, "B":0} for i in range(len(S))]
count_t = [{"A": 0, "B":0} for i in range(len(T))]

for i in range(len(S)):
  if(S[i] == "A"):
    count_s[i]["A"] += 1
    if(i == len(S) -1):
      break
    count_s[i+1] = count_s[i].copy()
  else:
    count_s[i]["B"] += 1
    if(i == len(S) -1):
      break
    count_s[i+1] = count_s[i].copy()


for i in range(len(T)):
  if(T[i] == "A"):
    count_t[i]["A"] += 1
    if(i == len(T) -1):
      break
    count_t[i+1] = count_t[i].copy()
  else:
    count_t[i]["B"] += 1
    if(i == len(T) -1):
      break
    count_t[i+1] = count_t[i].copy()


for i in range(q):
  if(S[A[i][0] - 1] == "A"):
    count_S = {"A": count_s[A[i][1] - 1]["A"] - count_s[A[i][0] - 1]["A"] + 1, "B": count_s[A[i][1] - 1]["B"] - count_s[A[i][0] -1]["B"]}
  else:
    count_S = {"A": count_s[A[i][1] - 1]["A"] - count_s[A[i][0] - 1]["A"], "B": count_s[A[i][1] - 1]["B"] - count_s[A[i][0] -1]["B"] + 1}
  if(T[A[i][2] - 1] == "A"):
    count_T = {"A": count_t[A[i][3] - 1]["A"] - count_t[A[i][2] - 1]["A"] + 1, "B": count_t[A[i][3] - 1]["B"] - count_t[A[i][2] - 1]["B"]}
  else:
    count_T = {"A": count_t[A[i][3] - 1]["A"] - count_t[A[i][2] - 1]["A"], "B": count_t[A[i][3] - 1]["B"] - count_t[A[i][2] - 1]["B"] + 1}

  if(count_S["A"] - count_T["A"] > 0):
    tmp = count_S["A"] - count_T["A"] - (count_S["B"] - count_T["B"])
    if(tmp % 3 == 0):
      print("YES")
    else:
      print("NO")

  else:
    tmp = count_T["A"] - count_S["A"] - (count_T["B"] - count_S["B"])
    if(tmp % 3 == 0):
      print("YES")
    else:
      print("NO")

Solution (Python):



if __name__ == "__main__":
    S = raw_input()
    T = raw_input()
    q = int(raw_input())

    S_sum = [0] * (len(S) + 1)
    T_sum = [0] * (len(T) + 1)

    for i, char in enumerate(S):
        if char == "A":
            S_sum[i+1] = S_sum[i] + 1
        elif char == "B":
            S_sum[i+1] = S_sum[i] + 2

    for i, char in enumerate(T):
        if char == "A":
            T_sum[i+1] = T_sum[i] + 1
        elif char == "B":
            T_sum[i+1] = T_sum[i] + 2

    for query in range(q):
        a, b, c, d = map(int, raw_input().split(" "))

        S_mod = (S_sum[b] - S_sum[a-1]) % 3
        T_mod = (T_sum[d] - T_sum[c-1]) % 3

        if S_mod == T_mod:
            print "YES"
        else:
            print "NO"

Solution (Python):
import numpy as np

ss = [0] + np.fromiter(map(lambda c: 1 if c == 'A' else 2, input()), dtype=int).cumsum().tolist()
ts = [0] + np.fromiter(map(lambda c: 1 if c == 'A' else 2, input()), dtype=int).cumsum().tolist()
q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    print('YES' if (ss[b] - ss[a - 1]) % 3 == (ts[d] - ts[c - 1]) % 3 else 'NO')

Solution (Python):
S = input()
T = input()
q = int(input())

s = [0]
t = [0]
v = 0
for c in S:
	v = (v + (ord(c) - ord("A") + 1)) % 3
	s.append(v)
v = 0
for c in T:
	v = (v + (ord(c) - ord("A") + 1)) % 3
	t.append(v)
for i in range(q):
	a,b,c,d = map(int, input().split())
	x = (3 + s[b] - s[a-1]) % 3
	y = (3 + t[d] - t[c-1]) % 3
	if x == y:
		print("YES")
	else:
		print("NO")

Solution (Python):



from random import *

def readln():
    _res = list(map(int,str(input()).split(' ')))
    return _res

def calc(s):
    res = [0 for ch in s]
    prev = 0
    for i in range(0,len(s)):
        if s[i] == 'A':
            res[i] = (prev - 1) % 3
        else:
            if s[i] == 'B':
                res[i] = (prev + 1) % 3
        prev = res[i]
    return res

def sum(l,r,s):
    l = l - 1
    r = r - 1
    if l > 0:
        res =  s[r] - s[l - 1]
    else:
        res =  s[r]
    return (res + 30000) % 3

s = input()
t = input()
ss = calc(s)
st = calc(t)
q = readln()[0]
for i in range(0,q):
    a,b,c,d = readln()
    x = sum(a,b,ss)
    y = sum(c,d,st)
    if x == y :
        print('YES')
    else:
        print('NO')

Solution (Python):
s = input()
t = input()
q = int(input())

ss, tt = [0], [0]
for i in range(len(s)):
    ss.append(ss[i] + (1 if s[i] == 'A' else 2))
for i in range(len(t)):
    tt.append(tt[i] + (1 if t[i] == 'A' else 2))

for i in range(q):
    a, b, c, d = map(int, input().split())
    s_score = (ss[b] - ss[a-1]) % 3
    t_score = (tt[d] - tt[c-1]) % 3
    print('YES' if s_score == t_score else 'NO')

Solution (Python):
s=input()
t=input()
ss=[0]
for i in range(len(s)):
    ss.append(ss[i] + (1 if s[i] == 'A' else 2))
tt=[0]
for i in range(len(t)):
    tt.append(tt[i] + (1 if t[i] == 'A' else 2))
q=int(input())
for _ in range(q):
    a,b,c,d=map(int,input().split())
    x = (ss[b]-ss[a-1]) % 3
    y = (tt[d]-tt[c-1]) % 3
    print('YES' if x == y else 'NO')

Solution (Python):
def check(ssum, tsum, a, b, c, d):
    return ((ssum[b] - ssum[a - 1]) - (tsum[d] - tsum[c - 1])) % 3 == 0

s = input()
t = input()
q = int(input())
a = [[int(i) for i in input().split()] for j in range(q)]

ssum = [0]
tsum = [0]
for i in range(len(s)):
    if s[i] == 'A':
        temp = 1
    else:
        temp = 2
    ssum.append(ssum[i] + temp)
for i in range(len(t)):
    if t[i] == 'A':
        temp = 1
    else:
        temp = 2
    tsum.append(tsum[i] + temp)

for i in range(q):
    if check(ssum, tsum, a[i][0], a[i][1], a[i][2], a[i][3]):
        print('YES')
    else:
        print('NO')

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    s = S()
    t = S()
    sl = len(s)
    tl = len(t)
    c = 0
    sa = []
    for i in range(sl):
        c += 1 if s[i] == 'A' else 2
        sa.append(c)
    c = 0
    ta = []
    for i in range(tl):
        c += 1 if t[i] == 'A' else 2
        ta.append(c)

    r = []
    q = I()
    for _ in range(q):
        a,b,c,d = LI()
        st = sa[b-1]
        if a > 1:
            st -= sa[a-2]
        tt = ta[d-1]
        if c > 1:
            tt -= ta[c-2]
        if st % 3 == tt % 3:
            r.append('YES')
        else:
            r.append('NO')


    return '\n'.join(r)



print(main())

Solution (Python):
import sys

def solve():
    S = input()
    T = input()

    ss = [0] * (len(S) + 1)

    for i in range(len(S)):
        ss[i + 1] = ss[i]
        ss[i + 1] += 1 if S[i] == 'A' else 2

    ts = [0] * (len(T) + 1)

    for i in range(len(T)):
        ts[i + 1] = ts[i]
        ts[i + 1] += 1 if T[i] == 'A' else 2

    q = int(input())

    for qry in range(q):
        a, b, c, d = map(int, sys.stdin.readline().split())
        a, c = a-1, c-1

        sc = (ss[b] - ss[a]) % 3
        tc = (ts[d] - ts[c]) % 3

        if sc == tc:
            ans = 'YES'
        else:
            ans = 'NO'

        print(ans)


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()

Solution (Python):
s = input()
t = input()
n = int(input())

a = [[0 for i in range(4)] for j in range(n)]
for i in range(n):
    a[i] = [int(j)-1 for j in input().split()]

def henkan(inp, n):
    o = [0] * n
    for i in range(n):
        if inp[i] == 'A':
            o[i] = 1
        else:
            o[i] = 2
    return o

def ruiseki(a, n):
    s = [0] * n
    s[0] = a[0]
    for i in range(1,n):
        s[i] =  s[i-1] + a[i]
    return s

def partSum(s, l, r):
    if l == 0:
        return s[r]
    return s[r] - s[l-1]

s_i = henkan(s,len(s))
t_i = henkan(t,len(t))

sum_s = ruiseki(s_i, len(s_i))
sum_t = ruiseki(t_i, len(t_i))

for i in range(n):
    x = partSum(sum_s, a[i][0], a[i][1])
    y = partSum(sum_t, a[i][2], a[i][3])
    if x%3 == y%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
S = raw_input()
T = raw_input()
n = int(raw_input())
q = []
for i in range(n):
    abcd = map(int, raw_input().split())
    q.append(abcd)

toi = lambda c: 1 if c == 'A' else 2
s = [0] * (len(S)+1)
for i in range(len(S)):
    s[i+1] = s[i] + toi(S[i])

t = [0] * (len(T)+1)
for i in range(len(T)):
    t[i+1] = t[i] + toi(T[i])

for a,b,c,d in q:
    si = (s[b] - s[a-1]) % 3
    ti = (t[d] - t[c-1]) % 3
    if si == ti:
        print 'YES'
    else:
        print 'NO'

Solution (Python):
S = list(input())
T = list(input())




q = int(input())

l = []
for i in range(q):
	l.append(list(map(int,input().split(" "))))


sta = [0 for i in range(len(S))]
stb = [0 for i in range(len(S))]

tta = [0 for i in range(len(T))]
ttb = [0 for i in range(len(T))]

if S[0] == "A":
	sta[0] = 1
else:
	stb[0] = 1

if T[0] == "A":
	tta[0] = 1
else:
	ttb[0] = 1

for i in range(1,len(S)):
	if S[i] == "A":
		sta[i] = sta[i-1]+1
		stb[i] = stb[i-1]
	else:
		sta[i] = sta[i-1]
		stb[i] = stb[i-1]+1

for i in range(1,len(T)):
	if T[i] == "A":
		tta[i] = tta[i-1]+1
		ttb[i] = ttb[i-1]
	else:
		tta[i] = tta[i-1]
		ttb[i] = ttb[i-1]+1


def get(l,a,b):
	if a == 0:
		return(l[b])
	else:
		return(l[b]-l[a-1])

for a,b,c,d in l:
	sa = get(sta,a-1,b-1)
	sb = get(stb,a-1,b-1)
	ta = get(tta,c-1,d-1)
	tb = get(ttb,c-1,d-1)


	if (sa+sb*2)%3 == (ta+tb*2)%3:
		print("YES")
	else:
		print("NO")

Solution (Python):
from sys import stdin
a = stdin.readline().strip()
b = stdin.readline().strip()
x = [0]
for i in a:
    x.append(x[-1])
    if i=='A':
        x[-1]+=1
    else:
        x[-1] += 2
y = [0]
for i in b:
    y.append(y[-1])
    if i=='A':
        y[-1] += 1
    else:
        y[-1] += 2
    
q = int(stdin.readline())
for _ in xrange(q):
    x1,y1,x2,y2 = map(int,stdin.readline().split())
    fir = x[y1] - x[x1-1];
    sec = y[y2] - y[x2-1];
    fir%=3
    sec%=3
    fir = (fir + 3)%3
    sec = (sec + 3)%3
    if fir==sec:
        print 'YES'
    else:
        print 'NO'

Solution (Python):
S = list(input())
T = list(input())




q = int(input())

l = []
for i in range(q):
	l.append(list(map(int,input().split(" "))))


sta = [0 for i in range(len(S))]
stb = [0 for i in range(len(S))]

tta = [0 for i in range(len(T))]
ttb = [0 for i in range(len(T))]

if S[0] == "A":
	sta[0] = 1
else:
	stb[0] = 1

if T[0] == "A":
	tta[0] = 1
else:
	ttb[0] = 1

for i in range(1,len(S)):
	if S[i] == "A":
		sta[i] = sta[i-1]+1
		stb[i] = stb[i-1]
	else:
		sta[i] = sta[i-1]
		stb[i] = stb[i-1]+1

for i in range(1,len(T)):
	if T[i] == "A":
		tta[i] = tta[i-1]+1
		ttb[i] = ttb[i-1]
	else:
		tta[i] = tta[i-1]
		ttb[i] = ttb[i-1]+1


def get(l,a,b):
	if a == 0:
		return(l[b])
	else:
		return(l[b]-l[a-1])

for a,b,c,d in l:
	sa = get(sta,a-1,b-1)
	sb = get(stb,a-1,b-1)
	ta = get(tta,c-1,d-1)
	tb = get(ttb,c-1,d-1)


	if (sa+sb*2)%3 == (ta+tb*2)%3:
		print("YES")
	else:
		print("NO")

Solution (Python):
s=raw_input()
t=raw_input()
sb=[0]*(len(s)+1)
tb=[0]*(len(t)+1)
for i in xrange(len(s)):
    if s[i]=="B":
        sb[i+1]+=1
for i in xrange(len(s)):
    sb[i+1]+=sb[i]
for i in xrange(len(t)):
    if t[i]=="B":
        tb[i+1]+=1
for i in xrange(len(t)):
    tb[i+1]+=tb[i]
q=int(raw_input())
for i in xrange(q):
    a,b,c,d=map(int,raw_input().split())
    a-=1;c-=1;
    cnt1=b-a;cnt2=d-c
    cnt1+=sb[b]-sb[a]
    cnt2+=tb[d]-tb[c]
    if abs(cnt1-cnt2)%3==0:
        print("YES")
    else:
        print("NO")

Solution (Python):
S = input()
T = input()
q = int(input())

SA = []
SB = []
cnt_a = 0
cnt_b = 0
for s in S:
    if s == "A":
        cnt_a += 1
    else:
        cnt_b += 1
    SA.append(cnt_a)
    SB.append(cnt_b)

TA = []
TB = []
cnt_a = 0
cnt_b = 0
for t in T:
    if t == "A":
        cnt_a += 1
    else:
        cnt_b += 1
    TA.append(cnt_a)
    TB.append(cnt_b)


def check(a, b, c, d):
    sa = SA[b-1]
    sb = SB[b-1]
    if a >= 2:
        sa -= SA[a - 2]
        sb -= SB[a - 2]
    ta = TA[d-1]
    tb = TB[d-1]
    if c >= 2:
        ta -= TA[c-2]
        tb -= TB[c-2]
    """
    print(sa, sb)
    print(ta, tb)
    """

    sb += 2 * sa
    sb %= 3

    tb += 2 * ta
    tb %= 3

    return sb == tb


for i in range(q):
    a, b, c, d = map(int, input().split())
    if check(a, b, c, d):
        print("YES")
    else:
        print("NO")

Solution (Python):

S = raw_input()
T = raw_input()

N, M = len(S), len(T)
SA, SB = [0]*(N + 2), [0]*(N + 1)
TA, TB = [0]*(M + 1), [0]*(M + 1)
for i in xrange(1, N + 1):
    SA[i] = SA[i - 1] + (1 if S[i - 1] == "A" else 0)
    SB[i] = SB[i - 1] + (1 if S[i - 1] == "B" else 0)
for i in xrange(1, M + 1):
    TA[i] = TA[i - 1] + (1 if T[i - 1] == "A" else 0)
    TB[i] = TB[i - 1] + (1 if T[i - 1] == "B" else 0)

q = int(raw_input())
for loop  in xrange(q):
    a, b, c, d = map(int, raw_input().split())
    A0 = SA[b] - SA[a - 1]
    B0 = SB[b] - SB[a - 1]
    A1 = TA[d] - TA[c - 1]
    B1 = TB[d] - TB[c - 1]
    A0 += 2*B0
    A1 += 2*B1
    A0 %= 3
    A1 %= 3

    if A0 == A1:
        print "YES"
    else:
        print "NO"

Solution (Python):
S = input()
T = input()
q = int(input())
ans = []

cnt = 0
sbs = [0]
for c in S:
    if c == 'B':
        cnt += 1
    sbs.append(cnt)
cnt = 0
tbs = [0]
for c in T:
    if c == 'B':
        cnt += 1
    tbs.append(cnt)

for _ in range(q):
    a, b, c, d = map(int, input().split())
    sb = sbs[b] - sbs[a - 1]
    tb = tbs[d] - tbs[c - 1]
    sa = (b - a) + sb
    if tb % 2 == 0:
        ta = (d - c - tb) + tb // 2
    else:
        ta = (d - c - tb) + (tb + 3) // 2
    if (sa - ta) % 3 == 0:
        ans.append('YES')
    else:
        ans.append('NO')

for yn in ans:
    print(yn)

Solution (Python):
import sys

cin = sys.stdin



S = cin.readline().strip()
T = cin.readline().strip()
q = int(cin.readline())

def ABcnt(S):
    ls = len(S)
    cnt = 0
    Rcnt = [0]*(ls+1)
    for i in range(ls):
        if S[i]=='A':
            cnt += 1
        else:
            cnt -= 1
        Rcnt[i+1] = cnt
    return Rcnt

Scnt = ABcnt(S)
Tcnt = ABcnt(T)

for i in range(q):
    a,b,c,d = map(int, cin.readline().split())
    Stotal = (Scnt[b]-Scnt[a-1]) % 3
    Ttotal = (Tcnt[d]-Tcnt[c-1]) % 3
    if Stotal==Ttotal:
        print('YES')
    else:
        print('NO')

Solution (Python):
NONE_RES,A_RES,B_RES = 0,1,2
buf = [[NONE_RES,B_RES,A_RES],[A_RES,NONE_RES,B_RES],[B_RES,A_RES,NONE_RES]]

S = input()
s = [1 if c == 'A' else 0 for c in S]
T = input()
t = [1 if c == 'A' else 0 for c in T]
n = int(input())

sum_s = [0]
for s_ in s:
    sum_s.append(sum_s[-1]+s_)

sum_t = [0]
for t_ in t:
    sum_t.append(sum_t[-1]+t_)

for _ in range(n):
    a,b,c,d = map(int,input().split(' '))
    A1 = sum_s[b] - sum_s[a-1]
    B1 = (b-(a-1))-A1
    A2 = sum_t[d] - sum_t[c-1]
    B2 = (d-(c-1))-A2
    type1 = buf[A1%3][B1%3]
    type2 = buf[A2%3][B2%3]
    if type1==type2:
        print('YES')
    else:
        print('NO')

Solution (Python):
s = input().strip()
t = input().strip()

csa = [0]
csb = [0]
for ch in s:
  if ch == "A":
    csa.append(csa[-1]+1)
    csb.append(csb[-1])
  else:  
    csa.append(csa[-1])
    csb.append(csb[-1]+1)
cta = [0]
ctb = [0]
for ch in t:
  if ch == "A":
    cta.append(cta[-1]+1)
    ctb.append(ctb[-1])
  else:  
    cta.append(cta[-1])
    ctb.append(ctb[-1]+1)
    
Q = int(input())
for q in range(Q):
  a, b, c, d = map(int, input().split())
  sa = csa[b] - csa[a-1]
  sb = csb[b] - csb[a-1]
  ta = cta[d] - cta[c-1]
  tb = ctb[d] - ctb[c-1]
  if (sa - sb + tb - ta) % 3:
    print("NO")
  else:  
    print("YES")