Problem: We have a string S of length N consisting of `R`, `G`, and `B`.  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:    * S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.   * j - i \neq k - j.

Submission attempts (in order):
N = int(input())
S = input()
r = 0
g = 0
b = 0
k = 0
for i in range(N):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1

sum = r * g * b
for i in range(0, N-2):
    for j in range(i+1,N-1):
        if 2*j - i < N:
            k = 2*j - i
        if S[j] == S[i] and S[k] == S[j]:
            sum -= 1

print(sum)


N = int(input())
S = "A"
S += input()
print(S)
r = 0
g = 0
b = 0
k = 0
for i in range(1, N+1):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1

sum = r * g * b
for i in range(1, N-1):
    for j in range(i+1,N):
        if 2*j - i <= N:
            k = 2*j - i
            if S[j] != S[i] and S[k] != S[j] and S[i] != S[k]:
                sum -= 1

print(sum)


N = int(input())
S = input()


#    for j in range(i+1,N-1):
#        if S[i] != S[j]:
#            for k in range(j+1,N):

#                    if S[j] != S[k] and S[k] != S[i]:
#                        count += 1
#print(count)

r = list()
g = list()
b = list()

for i in range(N):
    if S[i] == "R":
        r.append(i)
    elif S[i] == "G":
        g.append(i)
    else:
        b.append(i)

bb = len(b)
gg = len(g)
rr = len(r)

sum = len(r) * len(g) * len(b)
for i in range(bb-1):
    for j in range(i+1,gg):
        if 2*j - i <= 0:
            k = 2*j - i
        if j - i == k - j:
            sum -=1
        #if abs(abs(g[j] - b[i]) - b[i]) in r:
#        if (abs(b[i] - abs(g[j] - b[i])) in r) or (b[i] + abs(g[j] - b[i]) in r):

        #elif abs(abs(g[j] - b[i]) - g[j]) in r:
#        elif (abs(g[j] - abs(g[j] - b[i])) in r) or (g[j] + abs(g[j] - b[i]) in r):

#        elif (abs(r[j] - abs(r[j] - b[i])) in g) or (r[j] + abs(r[j] - b[i]) in g):

#        for k in range(rr):
#            if abs(r[k] - g[j]) == abs(g[j] - b[i]) or abs(g[j] - b[i]) == abs(b[i] - r[k]) or abs(b[i] - r[k]) == abs(r[k] - g[j]):
#                count += 1
print(sum)


N = int(input())
S = input()


#    for j in range(i+1,N-1):
#        if S[i] != S[j]:
#            for k in range(j+1,N):

#                    if S[j] != S[k] and S[k] != S[i]:
#                        count += 1
#print(count)

r = list()
g = list()
b = list()

for i in range(N):
    if S[i] == "R":
        r.append(i)
    elif S[i] == "G":
        g.append(i)
    else:
        b.append(i)

bb = len(b)
gg = len(g)
rr = len(r)

sum = len(r) * len(g) * len(b)
for i in range(bb-1):
    for j in range(i+1,gg):
        if 2*j - i <= 0:
            k = 2*j - i
        if i - j == k - j:
            sum -=1
        #if abs(abs(g[j] - b[i]) - b[i]) in r:
#        if (abs(b[i] - abs(g[j] - b[i])) in r) or (b[i] + abs(g[j] - b[i]) in r):

        #elif abs(abs(g[j] - b[i]) - g[j]) in r:
#        elif (abs(g[j] - abs(g[j] - b[i])) in r) or (g[j] + abs(g[j] - b[i]) in r):

#        elif (abs(r[j] - abs(r[j] - b[i])) in g) or (r[j] + abs(r[j] - b[i]) in g):

#        for k in range(rr):
#            if abs(r[k] - g[j]) == abs(g[j] - b[i]) or abs(g[j] - b[i]) == abs(b[i] - r[k]) or abs(b[i] - r[k]) == abs(r[k] - g[j]):
#                count += 1
print(sum)


N = int(input())
S = "A"
S += input()
print(S)
r = 0
g = 0
b = 0
k = 0
for i in range(1, N+1):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1

sum = r * g * b
for i in range(1, N-1):
    for j in range(i+1,N):
        if 2*j - i <= N:
            k = 2*j - i
        if S[j] == S[i] and S[k] == S[j] and S[i] == S[k]:
            sum -= 1

print(sum)


N = int(input())
S = "A"
S += input()
r = 0
g = 0
b = 0
k = 0
for i in range(1, N+1):
    if S[i] == "R":
        r += 1
    elif S[i] == "G":
        g += 1
    else:
        b += 1

sum = r * g * b
for i in range(1, N-1):
    for j in range(i+1,N):
        k = 2*j - i
        if k <= N:
            if S[j] != S[i] and S[k] != S[j] and S[i] != S[k]:
                sum -= 1

print(sum)


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Accepted