Problem:


Score : 500 points

Problem Statement

You have a string A = A_1 A_2 ... A_n consisting of lowercase English letters.

You can choose any two indices i and j such that 1 \leq i \leq j \leq n and reverse substring A_i A_{i+1} ... A_j.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

1 \leq |A| \leq 200,000

A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.

Sample Input 1

aatt

Sample Output 1

5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).

Sample Input 2

xxxxxxxxxx

Sample Output 2

1

Whatever substring you reverse, you'll always get xxxxxxxxxx.

Sample Input 3

abracadabra

Sample Output 3

44



Sample input:
aatt


Sample output:
5

Submission status: Wrong Answer

Solution code:
import itertools
txt = list(input())
count = 0
for i in range(len(txt)):
  for j in range(i+1, len(txt)):
    if txt[i:j+1] == list(reversed(txt[i:j+1])):
      #print(txt[i:j+1], list(reversed(txt[i:j+1])))
      count += 1
print((len(txt)-1)*(len((txt))-2)//2-count+1)

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)



def solve(n=None, s1=None, s2=None):
    s = getS()
    cnt = Counter(s)
    n = len(s)
    ans = 0
    for k, v in cnt.items():
        ans += v * (n - v)
        ans %= MOD

    ans //= 2
    ans += 1

    print(ans)


def main():
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)



def solve(n=None, s1=None, s2=None):
    s = getS()
    cnt = Counter(s)
    n = len(s)
    ans = 0
    for k, v in cnt.items():
        ans += v * (n - v)
        ans %= MOD

    ans *= divide(2)
    ans += 1
    ans %= MOD
    print(ans)


def main():
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
A = input().strip()
N = len(A)
tot = 1+(N*(N-1))//2
cnt = 0
for i in range(N-1):
    if A[i]==A[i+1]:
        cnt += 1
    k = 1
    while i-k>=0 and i+1+k<N:
        if A[i-k]==A[i+1+k]:
            cnt += 1
        else:break
        k += 1
for i in range(N-2):
    if A[i]==A[i+2]:
        cnt += 1
    k = 1
    while i-k>=0 and i+2+k<N:
        if A[i-k]==A[i+2+k]:
            cnt += 1
        else:break
        k += 1
print(tot-cnt)

Submission status: Wrong Answer

Solution code:
a = list(input())
n = len(a)

l = [0]*(2*n-1)
i = 0
j = 0
while i < 2*n - 1:
    while i >= j and i+j+1 < 2*n and a[(i-j)//2] == a[(i+j+1)//2]:
        j += 1
    l[i] = j
    k = 1
    while i + k < 2*n-1 and i >= k and l[i] >= k and l[i-k] != l[i]-k:
        l[i+k] = min(l[i-k],l[i]-k)
        k += 1
    i += k
    if j > k:
        j -= k
    else:
        j = 0
ans = n*(n-1)//2+1
for i in l:
    ans -= i//2
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

A = input()
N = len(A)

ans = N*(N-1)//2

for v in Counter(A).values():
	ans -= v*(v+1)//2

print(ans+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
S = S()
N = len(S)

i = j = 0
def manacher(s):
  N = len(s)
  R = [None for i in range(N)]
  i = j = 0
  while i < N:
    while i-j >= 0 and i+j < N and s[i-j] == s[i+j]:
      j += 1
    R[i] = j
    k = 1
    while i-k >= 0 and k+R[i-k] < j:
      R[i+k] = R[i-k]
      k += 1
    i += k
    j -= k
  return R

SS = ["*"]
for s in S:
  SS.append(s)
  SS.append("*")

R = manacher(SS)

E = [i//2 for i in R[2:-2:2]]
O = [i//2 for i in R[1:-1][::2]]

ans = N*(N+1)//2
for i in E:
  ans -= i
for i in O:
  ans -= i
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    A=input()
    ac=list(Counter().values())
    len_a=len(A)
    ans=len_a*(len_a-1)//2
    for acc in ac:
        ans-=acc*(acc-1)//2
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
c = Counter(s)
ans = len(s) * (len(s) - 1) // 2
for v in c.values():
    ans -= v * (v - 1) // 2
print(min(len(s) * (len(s) - 1) // 2, ans + 1))

Submission status: Wrong Answer

Solution code:
s = input()
cnt = [0]*26
n = len(s)
for i in range(n):
  cnt[ord(s[i])-97] += 1
ans = n*(n+1)//2
for i in range(26):
  ans -= (cnt[i]*(cnt[i]+1)//2)*(cnt[i]>1)
print(ans+1)

Submission status: Wrong Answer

Solution code:
a = input()
cnt = 0
for i in range(len(a)):
    for j in range(i+1, len(a)+1):
        s = a[i:j]
        if s!=s[::-1]:
            cnt += 1
print(cnt + 1)

Submission status: Wrong Answer

Solution code:
A = input()
n = len(A)
if n%2:
    ans = (n-1)*(n-2)//2+1
else:
    ans = (n-1)*n//2+1
ec = 0
oc = 0
enow = 0
while enow < n-1:
    if A[enow] == A[enow+1]:
        ec += 1
        nex = 1
        while enow+nex+1 < n and enow-nex >= 0:
            if A[enow+nex+1] == A[enow-nex]:
                ec += 1
                nex += 1
            else:
                break
    if enow >= 1:
        if A[enow-1] == A[enow+1]:
            oc += 1
            nex = 1
            while enow+nex+1 < n and enow-nex-1 >= 0:
                if A[enow+nex+1] == A[enow-nex-1]:
                    oc += 1
                    nex += 1
                else:
                    break
    enow += 1
print(ans - ec-oc)

Submission status: Wrong Answer

Solution code:

import numpy as np

dic={}
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'

A = input()
for a in ascii_lowercase:
    dic[a]=[0]*len(A)

i=0
for l in A:
    dic[l][i]=1
    i+=1

for d in dic:
    dic[d]=np.cumsum(dic[d])



dp=[0]*len(A)
dp[0]=1
for i in range(1,len(A)):
    tmp = (i+1-dic[A[i]][i])
    print(tmp)
    dp[i]=dp[i-1]+tmp

print(dp[-1])

Submission status: Wrong Answer

Solution code:
s=str(input())
n=len(s)
c=0
l=[]
for i in range(n):
    for j in range(i+1,n):
        t=s[i:j+1]
        l.append(s[:i]+t[::-1]+s[j+1:])
        if s[i:j+1]==t[::-1]:
            c+=1
            #print(s[i:j+1])
#print(1+n*(n-1)//2-c)
#l.sort()
#print(l)
print(len(set(l)))

Submission status: Wrong Answer

Solution code:
A=input()
ans=0
a=A.count("a")
b=A.count("b")
c=A.count("c")
d=A.count("d")
e=A.count("e")
f=A.count("f")
g=A.count("g")
h=A.count("h")
i=A.count("i")
j=A.count("j")
k=A.count("k")
l=A.count("l")
m=A.count("m")
n=A.count("n")
o=A.count("o")
p=A.count("p")
q=A.count("q")
r=A.count("r")
s=A.count("s")
t=A.count("t")
u=A.count("u")
v=A.count("v")
w=A.count("w")
x=A.count("x")
y=A.count("y")
z=A.count("z")
B=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
for aa in range(26):
  ans=ans+B[aa]*(len(A)-B[aa])
print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
import collections
dics=collections.Counter(s)
num=len(s)
ans=num*(num-1)//2
for i in dics.values():
  ans-=(i*(i-1)//2)
print(ans)

Submission status: Wrong Answer

Solution code:
strs = input()

N = 1
aaa = set()
for i in range(2, len(strs)+1):
    for j in range(len(strs)- i+1):
        strspart = strs[j:j + i]
        aaa.add(strs[0:j] +strspart[::-1] + strs [j+i:])
        
        
        

print(len(aaa))

Submission status: Wrong Answer

Solution code:
a = input()
count = 1
for left in range(len(a)):
    for right in range(left + 1, len(a)):
        if a[left:right + 1] != "".join(list(reversed(a[left:right + 1]))):
            count += 1
        else:
            print(a[left:right + 1])
print(count)

Submission status: Wrong Answer

Solution code:
a = input()
a_set = set([])
for i in range(len(a)):
    for j in range(i+1, len(a)):
        if a[:i] + "".join(list(reversed(a[i:j+1]))) + a[j+1:] not in a_set:
            a_set.add(a[:i] + "".join(list(reversed(a[i:j+1]))) + a[j+1:])
print(len(a_set))

Submission status: Wrong Answer

Solution code:
from collections import Counter
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    N = len(S)
    C = Counter(S)
    ans = 0
    for v in C.values():
        ans += v*(v-1)//2
    print(N*(N-1)//2-ans)


solve()

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    N = len(S)

    palindrome = 0
    for i in range(N-1):
        for j in range(i+2, N+1):
            if check(S[i:j]):
                #print("{} is pali".format(S[i:j]))
                palindrome += 1

    return N*(N-1)//2-palindrome+1


def check(S):
    '''
    ret True if S is palindrome
    '''
    return all(c == r for c, r in zip(S, reversed(S)))


print(solve())

Submission status: Wrong Answer

Solution code:

import sys
from collections import deque
import copy
import math

class manacher:
    """Cache radius array of palindrome to judge in linear time."""

    def __init__(self, l):
        """Insert '&' to judge both even and odd length palindrome."""
        self.n = len(l)
        self.m = 2*self.n+1
        self.d = ['&']*self.m
        self.r = [0]*self.m
        for i in range(self.n):
            self.d[2*i+1] = l[i]
        self.make_r()

    def make_r(self):
        """Make self.r[i] (radius of palindrome whose center is self.d[i])."""
        i, j = 0, 0
        while i < self.m:
            while j <= i < self.m-j and self.d[i-j] == self.d[i+j]:
                j += 1
            self.r[i] = j
            k = 1
            while k <= i < self.m-k and k+self.r[i-k] < j:
                self.r[i+k] = self.r[i-k]
                k += 1
            i += k
            j -= k

    def judge(self, start, end):
        """Return 1 if l[start, end) is a palindrome."""
        center = start+end
        return 2*end-1 < center+self.r[center]

    def get_num(self, center):
        return self.r[center]


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]

    mana = manacher(S)
    s = 0
    for i in range(0, len(S) * 2, 2):
        #print (mana.get_num(i) + mana.get_num(i + 1)) /2,
        s += (mana.get_num(i) + mana.get_num(i + 1)) /2
##    print
##    print mana.d
    print (len(S) * (len(S) + 1))/2 - s + 1


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
ans = N * (N - 1) // 2
a = []
for c in [chr(i) for i in range(97, 97+26)]:
    a.append(S.count(c))
ans -= sum(a)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
dp = [0]*26

for i in s:
  dp[ord(i) - ord('a')]+=1
#print(dp)
ans = 0
#重複する二つの文字分だけ減らして加える
for j in dp:
  ans += (j * (n-j))//2
print(ans+1)

Submission status: Wrong Answer

Solution code:
a = input()
ans = set()
for i in range(len(a)):
    for j in range(i + 1, len(a)):
        b = a[:i] + "".join(reversed(list(a[i:j + 1]))) + a[j + 1:]
        if b:
            ans.add(b)
print(len(ans))

Submission status: Wrong Answer

Solution code:
A = input()
N = len(A)


def palindromes(s):
    result = 0
    for i in range(len(s)):
        left = i - 1
        right = i + 1
        while 0 <= left and right < len(s) and s[left] == s[right]:
            result += 1
            left -= 1
            right += 1

        left = i
        right = i + 1
        while 0 <= left and right < len(s) and s[left] == s[right]:
            result += 1
            left -= 1
            right += 1

    return result


print(1 + N * (N - 1) // 2 - palindromes(A))

Submission status: Wrong Answer

Solution code:
ch = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
    'n','o','p','q','r','t','u','v','w','x','y','z']

p = []

a = list(input())
n = len(a)
for i in range(len(ch)):
    p.append(a.count(ch[i]))

res = n*(n-1)//2

for i in range(len(ch)):
    res = res - p[i] * (p[i]-1) //2
print(res+1)

Submission status: Wrong Answer

Solution code:
def main():
     s = list(input())
     count = (len(s)*len(s)-1)/2+1
     dic = {}
     for i in range(len(s)):
         try:
             dic[s[i]].append(i)
         except Exception:
             dic[s[i]] = [i]

     for j in dic.values():
         count-=(len(j)*len(j)-1)/2+1
     print(count)


if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def is_mirror(s) :
    for i in range(len(s)//2) :
        if s[i] != s[-(i+1)] :
            return False
    return True

def main() :
    a = input()
    L = len(a)
    ans = 1
    for i in range(L-1) :
        for j in range(L-1, i, -1) :
            if a[i] != a[j] :
                ans += 1
            else :
                if not is_mirror(a[i:j+1]) :
                    ans += 1

    print(ans)

main()

Submission status: Wrong Answer

Solution code:
str = list(input())
ans = 0
for i in range(len(str)):
    for j in range(i, len(str)+1, 1):
        if str[i:j] != list(reversed(str[i:j])):
            # print (str[i:j], list(reversed(str[i:j])))
            ans += 1
print (ans+1)

Submission status: Wrong Answer

Solution code:
a = list(input())

n = len(a)
cnt = {}
for c in a:
    if c in cnt:
        cnt[c] += 1
    else:
        cnt[c] = 1

ans = 1
for c in cnt:
    k = cnt[c]
    ans += k * (n - k) // 2

print(ans)

Submission status: Wrong Answer

Solution code:
from scipy.misc import comb


def main():
    s = input()
    seq = [0]*26
    for i in range(len(s)):
        seq[ord(s[i])-ord('a')] += 1

    cnt = len(s)*(len(s)-1)//2

    for i in filter(lambda x:x!=0, seq):
        cnt -= comb(i, 2)

    print(int(cnt)+1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))
def plis(a): return " ".join(list(map(str, a)))

alf = list("abcdefghijklmnopqrstuvwxyz")
A = rls()
n = len(A)
ans = n*(n-1)/2 + 1
for i in alf:
    count = 0
    for j in A:
        if(i == j):
            count += 1
    ans -= count*(count-1)/2

print(ans)

Submission status: Wrong Answer

Solution code:
a=input()
l=len(a)
b=l*(l-1)/2+1
for i in range(97,123):
    c=a.count(chr(i))
    d=(c*(c-1))//2
    b-=d
print(b)

Submission status: Wrong Answer

Solution code:
A = input()
l = len(A)
count = 1 + l*(l-1)//2
j = 1
if l <= 2:
    if l == 2:
        if A[1] == A[0]:
            count -= 1
else: 
    for i in range(2,l):
        if A[i] == A[i-1]:
            while i-j:
                if i+j == l:
                    break
                elif A[i+j] == A[i-j-1]:
                    j += 1
                else:
                    break
            
            count -= j
            j = 1
            
        if A[i] == A[i-2]:
            while i-j-1:
                if i+j == l:
                    break
                elif A[i+j] == A[i-j-2]:
                    j += 1
                else:
                    break
            
            count -= j
            j = 1

print(count)

Submission status: Wrong Answer

Solution code:
import math

A = input()
n = len(A)

if n == 1:
    print(1)
else:

    result = 0
    string = []

    for i in range(n):
        for j in range(min(i + 1, n - i - 1)):
            str1 = A[i - j:i + 1]
            str2 = A[i + 1:i + j + 2]
            if i - j == 0:
                ALL = str2 + str1 + A[i + j + 2:]
            else:
                ALL = A[:i - j] + str2 + str1 + A[i + j + 2:]
            if ALL not in string:
                string.append(ALL)
                result += 1

        for j in range(min(i, n - i - 1)):
            str1 = A[i - 1 - j:i]
            str2 = A[i + 1:i + 2 + j]
            if i - j == 0:
                ALL = str2 + A[i] + str1 + A[i + j + 2:]
            else:
                ALL = A[:i - j - 1] + str2 + A[i] + str1 + A[i + j + 2:]
            if ALL not in string:
                string.append(ALL)
                result += 1
    print(int(result))

Submission status: Wrong Answer

Solution code:
import math

A = input()
n = len(A)

if n == 1:
    print(1)
else:
    result = 1 + (math.factorial(n) / (math.factorial(2) * math.factorial(n - 2)))

    count = 0
    string = []

    for i in range(n):
        for j in range(min(i + 1, n - i - 1)):
            str1 = A[i - j:i + 1]
            str2 = A[i + 1:i + j + 2]
            if i - j == 0:
                ALL = str2 + str1 + A[i + j + 2:]
            else:
                ALL = A[:i - j] + str2 + str1 + A[i + j + 2:]
            # print(count, str1, str2, str1 == str2)
            count += 1
            # print(ALL)
            string.append(ALL)
            if str1 != str2:
                if ALL in string[:-2]:
                    result -= 1
                # break
                pass
            else:
                # print('delete', A[i - j:i + 1], A[i + 1:i + j + 2])
                result -= 1

        for j in range(min(i, n - i - 1)):
            str1 = A[i - 1 - j:i]
            str2 = A[i + 1:i + 2 + j]
            if i - j == 0:
                ALL = str2 + A[i] + str1 + A[i + j + 2:]
            else:
                ALL = A[:i - j - 1] + str2 + A[i] + str1 + A[i + j + 2:]
            # print(count, A[i - 1 - j: i + j + 2], str1, str2, str1 == str2)
            count += 1
            # print(ALL)
            string.append(ALL)
            if str1 != str2:
                if ALL in string[:-2]:
                    result -= 1
                # break
                pass
            else:
                # print('delete', str1, str2)
                result -= 1
    print(result)

Submission status: Wrong Answer

Solution code:
import math

A = input()
n = len(A)

if n == 1:
    print(1)
else:
    result = 1 + (math.factorial(n) / (math.factorial(2) * math.factorial(n - 2)))

    count = 0
    string = []

    for i in range(n):
        for j in range(min(i + 1, n - i - 1)):
            str1 = A[i - j:i + 1]
            str2 = A[i + 1:i + j + 2]
            if i - j == 0:
                ALL = str2 + str1 + A[i + j + 2:]
            else:
                ALL = A[:i - j] + str2 + str1 + A[i + j + 2:]
            # print(count, str1, str2, str1 == str2)
            count += 1
            # print(ALL)
            string.append(ALL)
            if str1 != str2:
                if ALL in string[:-2]:
                    result -= 1
                # break
                pass
            else:
                # print('delete', A[i - j:i + 1], A[i + 1:i + j + 2])
                result -= 1

        for j in range(min(i, n - i - 1)):
            str1 = A[i - 1 - j:i]
            str2 = A[i + 1:i + 2 + j]
            if i - j == 0:
                ALL = str2 + A[i] + str1 + A[i + j + 2:]
            else:
                ALL = A[:i - j - 1] + str2 + A[i] + str1 + A[i + j + 2:]
            # print(count, A[i - 1 - j: i + j + 2], str1, str2, str1 == str2)
            count += 1
            # print(ALL)
            string.append(ALL)
            if str1 != str2:
                if ALL in string[:-2]:
                    result -= 1
                # break
                pass
            else:
                # print('delete', str1, str2)
                result -= 1