Problem p02714 (python)

Fixed submission:
import sys
from io import StringIO
import unittest


def resolve():
    n = int(input())

    # 1文字ずつチェックするのでSET。
    s = list(input())

    # 全パターンの数(RGB1つずつ選択する場合)
    ans = s.count("R")*s.count("G")*s.count("B")

    # ここから条件(j-i≠k-j)を満たさないパターンを減算。
    s_len = len(s)
    minus = 0

    # i<=距離A=>j<=距離A=>k という状態で、距離Aが等しいパターンを列挙していく。
    for i in range(s_len-2):
        first = s[i]
        for j in range(1, 9999):

            # 文字数を超えるパターンは検証しない
            if i+j+j > s_len - 1:
                break

            # 1文字目,2文字目,3文字目が全部別の場合、除外するパターンである。(合計数から減算する)
            if first is not s[i+j] and first is not s[i+j+j] and s[i+j] is not s[i+j+j]:
                minus += 1

    print(ans - minus)




    # ans = 0
    # for i in range(len(s) - 2):
    #
    #     target = list("RGB".replace(s[i], ""))
    #     ans += s[i:len(s)].count(target[0]) * s[i:len(s)].count(target[1])
    #
    # for i in range(len(s) - 2)
    #
    #
    #
    #     for j in range(i+1, len(s) - 1):
    #         if s[i] is s[j]:
    #             continue
    #         target = "RGB".replace(s[i], "").replace(s[j], "")
    #
    #         ans += s[j:len(s)].count(target)
    #
    #         if j+(j-i) < len(s) :
    #             if s[j+(j-i)] is target:
    #                 ans -= 1
    #
    # print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

#     def test_入力例_3(self):
#         input = """4080
# RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBR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ime Error):
import sys
from io import StringIO
import unittest


def resolve():
    n = int(input())

    # 1文字ずつチェックするのでSET。
    s = list(input())

    # 全パターンの数(RGB1つずつ選択する場合)
    ans = s.count("R")*s.count("G")*s.count("B")

    # ここから条件(j-i≠k-j)を満たさないパターンを減算。
    s_len = len(s)
    minus = 0

    # i<=距離A=>j<=距離A=>k という状態で、距離Aが等しいパターンを列挙していく。
    for i in range(s_len-2):
        first = s[i]
        for j in range(1, 9999):

            # 文字数を超えるパターンは検証しない
            if i+j+j > s_len - 1:
                break

            # 1文字目,2文字目,3文字目が全部別の場合、除外するパターンである。(合計数から減算する)
            if first is not s[i+j] and first is not s[i+j+j] and s[i+j] is not s[i+j+j]:
                minus += 1

    print(ans - minus)




    # ans = 0
    # for i in range(len(s) - 2):
    #
    #     target = list("RGB".replace(s[i], ""))
    #     ans += s[i:len(s)].count(target[0]) * s[i:len(s)].count(target[1])
    #
    # for i in range(len(s) - 2)
    #
    #
    #
    #     for j in range(i+1, len(s) - 1):
    #         if s[i] is s[j]:
    #             continue
    #         target = "RGB".replace(s[i], "").replace(s[j], "")
    #
    #         ans += s[j:len(s)].count(target)
    #
    #         if j+(j-i) < len(s) :
    #             if s[j+(j-i)] is target:
    #                 ans -= 1
    #
    # print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

#     def test_入力例_3(self):
#         input = """4080
# RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBG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