def count_cpctf_substrings(s): n = len(s) count = 0 # 5つの位置を選ぶすべての組み合わせを考える for i in range(n): for j in range(i+1, n): for k in range(j+1, n): for l in range(k+1, n): for m in range(l+1, n): # 選んだ5文字 sub = [s[i], s[j], s[k], s[l], s[m]] # CPCTF的かどうかを判定 # 1文字目と3文字目が等しい if sub[0] == sub[2]: # それ以外の文字はすべて異なる if (sub[0] != sub[1] and sub[0] != sub[3] and sub[0] != sub[4] and sub[1] != sub[2] and sub[1] != sub[3] and sub[1] != sub[4] and sub[2] != sub[3] and sub[2] != sub[4] and sub[3] != sub[4]): count += 1 return count