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



Sample input:
BBBAAAABA
BBBBA
4
7 9 2 5
7 9 1 4
1 7 2 5
1 7 2 4


Sample output:
YES
NO
YES
NO

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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'

Submission status: Accepted

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

# S=list("BBAAABA")
# T=list("BBBA")

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")

Submission status: Accepted

Solution code:
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'

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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"

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys

cin = sys.stdin
# cin = open("in.txt", "r")


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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

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


def pre_(X):
    d= dict()
    d[0]=0
    tmp = 0
    for i,s in enumerate(X):
        if s=="A":
           tmp +=1
        else:
            tmp+=2
        d[i+1]=tmp
    return d

Sx = pre_(S)
Tx = pre_(T)


for a,b,c,d in [list(map(int,input().split())) for _ in range(int(input()))]:
    """
    print(Sx[a-1])
    print(Sx[b])
    print(Tx[c-1])
    print(Tx[d])
    """
    print( "YES" if (((Sx[a-1]-Sx[b])- (Tx[c-1]-Tx[d]))%3==0) else "NO")

Submission status: Accepted

Solution code:
import numpy as np
S = input()
T = input()
SA = np.array([0]+[s == 'A' for s in S]).cumsum()
SB = np.array([0]+[s == 'B' for s in S]).cumsum()
TA = np.array([0]+[s == 'A' for s in T]).cumsum()
TB = np.array([0]+[s == 'B' for s in T]).cumsum()

q = int(input())
res_list = []

for _ in range(q):
    a, b, c, d = map(int, input().split())
    a_cnt = ((SA[b] - SA[a-1]) - (TA[d] - TA[c-1])) % 3
    b_cnt = ((SB[b] - SB[a-1]) - (TB[d] - TB[c-1])) % 3
    if a_cnt == b_cnt:
        res_list.append('YES')
    else:
        res_list.append('NO')
    
for i in range(q):
    print(res_list[i])

Submission status: Accepted

Solution code:
S = raw_input()
T = raw_input()
s = [0]*(len(S)+1)
for i in range(len(S)):
    s[i+1] = s[i] + (-1 if S[i]=="A" else 1)
t = [0]*(len(T)+1)
for i in range(len(T)):
    t[i+1] = t[i] + (-1 if T[i]=="A" else 1)
q = input()
for _ in range(q):
    a, b, c, d = map(int, raw_input().split())
    print "YES" if (s[b]-s[a-1])%3==(t[d]-t[c-1])%3 else "NO"

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

s = input()
t = input()
q = int(input())
abcd = [list(map(int, input().split())) for _ in range(q)]

rmv = ["AAA", "BBB", "AB", "BA"]

dct = {"": 0, "A": 1, "B": 2, "AA": 2, "BB": 1}


def f(word):
    ln = len(word)
    n = 1
    while n < ln:
        n *= 2

    li = [""] * (2 * n - 1)
    for i, e in enumerate(word, n - 1):
        li[i] = e

    for i in range(n - 2, -1, -1):
        l = i * 2 + 1
        r = i * 2 + 2
        li[i] = li[l] + li[r]
        for e in rmv:
            li[i] = li[i].replace(e, "")

    return li, n


def make(data, begin, end, i, l, r):
    if r <= begin or end <= l:
        return ""

    if begin <= l and r <= end:
        return data[i]

    wl = make(data, begin, end, i * 2 + 1, l, (l + r) // 2)
    wr = make(data, begin, end, i * 2 + 2, (l + r) // 2, r)
    ret = wl + wr
    for e in rmv:
        ret = ret.replace(e, "")
    return ret


ss, sr = f(s)
tt, tr = f(t)

for a, b, c, d in abcd:
    sss = make(ss, a - 1, b, 0, 0, sr)
    ttt = make(tt, c - 1, d, 0, 0, tr)
    if sss not in dct.keys() or ttt not in dct.keys():
        print("foo")
    if dct[sss] == dct[ttt]:
        print("YES")
    else:
        print("NO")

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

s = input()
t = input()
q = int(input())
abcd = [list(map(int, input().split())) for _ in range(q)]

rmv = ["AAA", "BBB", "AB", "BA"]

dct = {"": 0, "A": 1, "B": 2, "AA": 2, "BB": 1}


def f(word):
    ln = len(word)
    n = 1
    while n < ln:
        n *= 2

    li = [""] * (2 * n - 1)
    for i, e in enumerate(word, n - 1):
        li[i] = e

    for i in range(n - 2, -1, -1):
        l = i * 2 + 1
        r = i * 2 + 2
        li[i] = li[l] + li[r]
        for e in rmv:
            li[i] = li[i].replace(e, "")

    return li, n


def make(data, begin, end, i, l, r):
    if r <= begin or end <= l:
        return ""

    if begin <= l and r <= end:
        return data[i]

    wl = make(data, begin, end, i * 2 + 1, l, (l + r) // 2)
    wr = make(data, begin, end, i * 2 + 2, (l + r) // 2, r)
    ret = wl + wr
    for e in rmv:
        ret = ret.replace(e, "")
    return ret


ss, sr = f(s)
tt, tr = f(t)

for a, b, c, d in abcd:
    sss = make(ss, a - 1, b, 0, 0, sr)
    ttt = make(tt, c - 1, d, 0, 0, tr)
    if dct[sss] == dct[ttt]:
        print("YES")
    else:
        print("NO")

Submission status: Runtime Error

Solution code:
s = input()
t = input()
q = int(input())
abcd = [list(map(int, input().split())) for _ in range(q)]

rmv = ["AAA", "BBB", "AB", "BA"]

dct = {"": 0, "A": 1, "B": 2, "AA": 2, "BB": 1}


def f(word):
    ln = len(word)
    n = 1
    while n < ln:
        n *= 2

    li = [""] * (2 * n - 1)
    for i, e in enumerate(word, n - 1):
        li[i] = e

    for i in range(n - 2, -1, -1):
        l = i * 2 + 1
        r = i * 2 + 2
        li[i] = li[l] + li[r]
        for e in rmv:
            li[i] = li[i].replace(e, "")

    return li, n


def make(data, begin, end, i, l, r):
    if r <= begin or end <= l:
        return ""

    if begin <= l and r <= end:
        return data[i]

    wl = make(data, begin, end, i * 2 + 1, l, (l + r) // 2)
    wr = make(data, begin, end, i * 2 + 2, (l + r) // 2, r)
    ret = wl + wr
    for e in rmv:
        ret = ret.replace(e, "")
    return ret


ss, sr = f(s)
tt, tr = f(t)

for a, b, c, d in abcd:
    sss = make(ss, a - 1, b, 0, 0, sr)
    ttt = make(tt, c - 1, d, 0, 0, tr)
    if dct[sss] == dct[ttt]:
        print("YES")
    else:
        print("NO")

Submission status: Runtime Error

Solution code:
s = input()
t = input()
q = int(input())
abcd = [tuple(map(int,input().split())) for i in range(q)]

def to_one(word):
    if len(word)==0:
        return 0

    if len(word)==1:
        return word

    if word[:2] == 'AB' or word[:2] == 'BA':
        return to_one(word[2:])

    if word[:3] == 'AAA':
        return to_one(word[3:])

    if word[:3] == 'BBB':
        return to_one(word[3:])

    if word[:2] == 'AA':
        return to_one('B'+word[2:])
    if word[:2] == 'BB':
        return to_one('A'+word[2:])


for a,b,c,d in abcd:

    s_i = s[a-1:b]
    t_i = t[c-1:d]

    if to_one(s_i) == to_one(t_i):
        print('YES')
    else:
        print('NO')

Submission status: Runtime Error

Solution code:
import math
inp=lambda:(int,input().split())
s=input()
t=input()
q=int(input())
for _ in range(q):
	a,b,c,d=inp()
	if abs((s[a:b].count('A')-s[a:b].count('B')))==abs((t[a:b].count('A')-t[a:b].count('B'))):
		print('YES')
	else:
		print('NO')

Submission status: Runtime Error

Solution code:
S = input()
T = input()
N = len(S)
M = len(M)
Ss = [0]*(N+1)
Ts = [0]*(M+1)
for i in range(N):
  if S[i] == "A":
    temp = 1
  else:
    temp = -1
  Ss[i+1] = Ss[i] + temp
for i in range(M):
  if T[i] == "A":
    temp = 1
  else:
    temp = -1
  Ts[i+1] = Ts[i] + temp
Q = int(input())
for i in range(Q):
  a, b, c, d = (int(i) for i in input().split())
  if a == b:
    print("NO")
    continue
  else:
    if (Ss[b] - Ss[a-1] - Ts[d] + Ts[c-1])%3==0:
      print("YES")
    else:
      print("NO")

Submission status: Runtime Error

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

# S=list("BBAAABA")
# T=list("BBBA")

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))]
tta = [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
	else:
		stb[i] = stb[i-1]+1

for i in range(1,len(T)):
	if T[i] == "A":
		tta[i] = tta[i-1]+1
	else:
		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")