Problem:


Score: 400 points

Problem Statement

AtCoder Inc. has decided to lock the door of its office with a 3-digit PIN code.

The company has an N-digit lucky number, S. Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code.

How many different PIN codes can he set this way?

Both the lucky number and the PIN code may begin with a 0.

Constraints

4 \leq N \leq 30000

S is a string of length N consisting of digits.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of different PIN codes Takahashi can set.

Sample Input 1

4
0224

Sample Output 1

3

Takahashi has the following options:

Erase the first digit of S and set 224.

Erase the second digit of S and set 024.

Erase the third digit of S and set 024.

Erase the fourth digit of S and set 022.

Thus, he can set three different PIN codes: 022, 024, and 224.

Sample Input 2

6
123123

Sample Output 2

17

Sample Input 3

19
3141592653589793238

Sample Output 3

329



Sample input:
4
0224


Sample output:
3

Submission status: Time Limit Exceeded

Solution code:
import bisect
import collections

def read():
    N = int(input().strip())
    S = str(input().strip())
    return N, S

def dig(S, T, N):
    for i in range(N):
            if S[i] == T[0]:
                for j in range(i+1, N):
                    if S[j] == T[1]:
                        for k in range(j+1, N):
                            if S[k] == T[2]:
                                return True
    return False


def solve(N, S):
    d = [collections.deque() for i in range(10)]
    count = 0
    for p in range(1000):
        T = '{:03d}'.format(p)
        count += 1 if dig(S, T, N) else 0
        
    return count
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)

Submission status: Time Limit Exceeded

Solution code:
import itertools

#入力
N=int(input())
A = [int(x) for x in list(input())]
ans = 0

AA=[x for x in range(10)]


for i in AA:
    for j in AA:
        for k in AA:
            v=[i,j,k]
            now = 0
            for l in A:
                if (l == v[now]):
                    now += 1
                    if (now == 3):
                        #print(v)
                        break
            if (now == 3):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()


def exists(v):
    i = 0
    j = 0
    while i < N:
        if S[i] == v[j]:
            j += 1
        if j == 3:
            return True
        i += 1
    return False


ans = 0
for i in range(0, 1000):
    if i < 10:
        v = list('00' + str(i))
    elif i < 100:
        v = list('0' + str(i))
    else:
        v = list(str(i))
    if exists(v):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

dp = [[[0 for _ in range(1000)] for _ in range(4)] for _ in range(n+1)]
dp[0][0][0] = 1
ans = 0

for i in range(n):
    for j in range(4):
        for k in range(1000):
            if dp[i][j][k] == 0: continue
            dp[i+1][j][k] = 1
            if j <= 2:
                dp[i+1][j+1][k*10 + int(s[i])] = 1

for k in range(1000):
    ans += dp[n][3][k]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())

cnt = [0] * 1000
l = len(s)
for i in range(l-2):
    for j in range(i+1, l-1):
        for k in range(j+1, l):
            S = ''.join([s[i], s[j], s[k]])
            cnt[int(S)] += 1

ans = 0
for i in range(len(cnt)):
    if cnt[i] > 0:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
seq = list(set(list(s)))
genkai_list = []
for i in range(0,1000):
    if 0 <= i <= 9:
        ap = "00" + str(i)
        genkai_list.append(ap)
    elif 10 <= i <= 99:
        ap = "0" + str(i)
        genkai_list.append(ap)
    else:
        genkai_list.append(str(i))
ans = []
for a in range(len(genkai_list)):
    for i in range(len(s)):
        if s[i] == genkai_list[a][0]:
            for j in range(i + 1, len(s)):
                if s[j] == genkai_list[a][1]:
                    for k in range(j + 1, len(s)):
                        if s[k] == genkai_list[a][2]:
                            ans.append(a)
print(len(set(ans)))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()

D=[0]*1000

for i in range(1000):
    a=str(i//100)
    b=str((i//10)%10)
    c=str(i%10)
    f=0
    for j in S:
        if(f==2 and c==j):
            D[i]=1
            break
        elif(f==1 and b==j):
            f=2
        elif(f==0 and a==j):
            f=1
print(sum(D))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
new_s = input()
# new_s = ""
# for i in range(len(s)):
#    if i<3:
#        new_s+=s[i]
#     else:
#         if s[i-1] == s[i] and s[i-2] == s[i]:
#             pass
#         else:
#             new_s+=s[i]
pas = -1
ans = 0
for i in range(1000):
    ott = 0
    pas = int(pas)+1
    if pas < 10:
        pas = "00"+str(pas)
    elif pas < 100:
        pas = "0"+str(pas)
    pas = str(pas)
    idx = -1
    while idx < len(new_s)-1:
        idx += 1
        if ott == 0:
            if pas[0] == new_s[idx]:
                ott += 1
        elif ott == 1:
            if pas[1] == new_s[idx]:
                ott += 1
        elif ott == 2:
            if pas[2] == new_s[idx]:
                ott += 1
                ans += 1
        if ott == 3:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

a = []
for i in range(10):
    a.append(str(i))
    
ans = 0    
s = 0
for i in a:
    for j in a:
        for k in a:
            for l in range(N):
                if S[l] == i and s == 0:
                    s = 1
                elif S[l] == j and s == 1:
                    s = 2
                elif S[l] == k and s == 2:
                    ans += 1
                    break
            s = 0
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(int, list(input())))

ans = set()
for i in range(10):
  for j in range(10):
    for k in range(10):
      iflg = False
      jflg = False
      for s in S:
        if not iflg and s == i:
          iflg = True
        elif (not jflg) and iflg and s == j:
          jflg = True
          continue
        elif iflg and jflg and s == k:
          ans.add((i,j,k))
          break
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
for x in range(10):
    for y in range(10):
        for z in range(10):
            phase = 0
            for c in S:
                if phase == 0 and c == str(x):
                    phase = 1
                    continue
                if phase == 1 and c == str(y):
                    phase = 2
                    continue
                if phase == 2 and c == str(z):
                    count += 1
                    break
print(count)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
S = input()
ans = 0
anslst = []
for i in range(10):
    for j in range(10):
        for k in range(10):
            anslst.append(str(i) + str(j) + str(k))

tmp = ans
for i in anslst:
    for j in range(N):
        if i[0] == S[j]:
            for k in range(j + 1, N):
                if i[1] == S[k]:
                    for n in range(k + 1, N):
                        if i[2] == S[n]:
                            ans += 1
                            break
                    break
        if tmp != ans:
            tmp = ans
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#dp[i]:i+1桁目-n桁目で1桁の数字は/2桁の数字は/何通りあるか
n = int(input())
s = input()+"a"
ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            t1 = n-1
            u1 = n-1
            v1 = n-1
            for t in range(n):
                if i == int(s[t]):
                    t1 = t
                    break
            for u in range(t1+1,n):
                if j == int(s[u]):
                    u1 = u
                    break
            for v in range(u1+1,n):
                if k == int(s[v]):
                    v1 = v
                    break
            if int(s[t1]) == i and int(s[u1]) == j and int(s[v1]) == k and t1<u1 and u1<v1:
                #print(i,j,k,t1,u1,v1)
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
###
n = int(input())
s = input()
ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            t1 = n-1
            u1 = n-1
            v1 = n-1
            for t in range(n):
                if i == int(s[t]):
                    t1 = t
                    break
            for u in range(t1+1,n):
                if j == int(s[u]):
                    u1 = u
                    break
            for v in range(u1+1,n):
                if k == int(s[v]):
                    v1 = v
                    break
            if int(s[t1]) == i and int(s[u1]) == j and int(s[v1]) == k and t1<u1 and u1<v1:
                #print(i,j,k,t1,u1,v1)
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

flag = False
count = 0

for i in range(0,1000):
    arr = [str("00"+str(i))[-j-1] for  j in range(3)]
    checkcount = 0
    for t in s:
        if arr[checkcount] == t:
            checkcount += 1
        if checkcount == 3:
            flag = True
            break
    if flag:
        count += 1
        flag = False
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())

a = []
for i in range(10):
    for j in range(10):
        for z in range(10):
            a.append([i,j,z])

s = list(S)
ans = 0
for n in a:
    for i,name in enumerate(s):
        if int(name) == n[0]:
            n = n[1:]
        if len(n) == 0:
            ans += 1
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

N = int(input())
S = [int(c) for c in input()]

a = [b for b in itertools.combinations(S, 3)]

print(len(get_unique_list(a)))

Submission status: Time Limit Exceeded

Solution code:
def solve(s1, s2, s3, S):
	flag = 0
	for i in range(len(S)):
		if flag == 0:
			if S[i] == s1:
				flag = 1
		elif flag == 1:
			if S[i] == s2:
				flag = 2
		elif flag == 2:
			if S[i] == s3:
				flag = 3
	if flag == 3:
		return True
	else:
		return False

if __name__ == "__main__":
	N = int(input())
	S = input()
	ans = 0
	for s1 in range(0, 10):
		for s2 in range(0, 10):
			for s3 in range(0, 10):
				if solve(str(s1), str(s2), str(s3), S):
					ans += 1
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
 
 
def solve(c, idx, N, S, ans):
    if len(c)==3:
        ans.append(c)
        return
    if idx >= len(S):
        return
    solve(c+S[idx], idx+1, N, S, ans)
    solve(c, idx+1, N, S, ans)
 
if __name__ == "__main__":
    N = int(input())
    S = input()
    ans = []
    solve("", 0, N, S, ans)
    print(len(set(ans)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0

for i in range(1000):
    a = i%10
    b = (i//10)%10
    c = (i//100)%10
    flag = 0
    for j in range(n):
        dum = s[j]
        if flag == 0 and dum == str(a):
            flag += 1
        elif flag == 1 and dum == str(b):
            flag += 1
        elif flag == 2 and dum == str(c):
            ans += 1
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])
    s = args[1]
    ret = 0
    for i in range(1000):
        iii = '{:03d}'.format(i)
        j = 0
        for k in range(n):
            if s[k] == iii[j]:
                j += 1
                if j == 3:
                    ret += 1
                    break

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
a=[1]*1000
for i in range(0,len(S)-2):
  for j in range(i+1,len(S)-1):
    for k in range(j+1,len(S)):
      a[int(S[i])*100+int(S[j])*10+int(S[k])]=0
print(1000-sum(a))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import copy

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

digit = [len(set(S[i+1:])) for i in range(N-1)]
ans = [0 for i in range(100)]
for i in range(N-2):
  for j in range(i+1, N-1):
    tmp = int(S[i])*10 + int(S[j])
    ans[tmp] = max(ans[tmp], (digit[j]))

print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
s = S()

cnt = 0
for a in range(10):
    for b in range(10):
        for c in range(10):
            n = 0
            abc = [a,b,c]
            for i in range(N):
                if int(s[i]) == abc[n]:
                    n += 1
                if n == 3:
                    cnt += 1
                    break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
s = [int(i) for i in input()]
a = {i for i in itertools.combinations(s, 3)}


print(len(a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())

list=list(S)

kei=[]
for a in range(N-2):
    for b in range(a+1,N-1):
        if b<=a:
            continue
        for c in range(b+1,N):
            if c<=b:
                continue
            #print(a,b,c)
            retsu=''
            retsu=retsu+list[a]+list[b]+list[c]
            kei.append(retsu)
last=set(kei)
print(len(last))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

numset = set([0,1,2,3,4,5,6,7,8,9])
resset = set()

for i in range(N):
    for j in range(i+1,N):
        useset = set(S[j+1:N])
        for k in useset:
            L = [S[i],S[j],k]
            temp = ''.join(L)
            resset.add(temp)

print(len(resset))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()

ans = 0
for i in range(1000):
    a = '{0:03d}'.format(i)
    f1, f2 = False, False
    for s in S:
        if not f1 and s == a[0]:
            f1 = True
            continue
        if f1 and not f2 and s == a[1]:
            f2 = True
            continue
        if f1 and f2 and s == a[2]:
            ans += 1
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt = 0

for v in range(1000):
    v = str(v).zfill(3)
    f = 0
    for i in range(n):
        if f == 2:
            if v[2] == s[i]:
                cnt += 1
                break
        elif f == 1:
            if v[1] == s[i]:
                f = 2
        else:
            if v[0] == s[i]:
                f = 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
sarr = list(map(int, input()))

ans = 0

for v1 in range(10):
    for v2 in range(10):
        for v3 in range(10):
            current = 0
            for (i, e) in enumerate(sarr):
                if e == v1:
                    current = i + 1
                    break
            else:
                continue

            for (i, e) in list(enumerate(sarr))[current:]:
                if e == v2:
                    current = i + 1
                    break
            else:
                continue

            for (i, e) in list(enumerate(sarr))[current:]:
                if e == v3:
                    ans += 1
                    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())

a = [[[0 for _ in range(10)] for _ in range(10)] for _ in range(10)]

s = list(S)
lens = len(s)
for i in range(lens):
    ni = int(s[i])
    for j in range(i+1, lens):
        nj = int(s[j])
        for k in range(j+1, lens):
            nk = int(s[k])
            a[ni][nj][nk] = 1

ans = sum(sum(a, []), [])
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
def solve(digits) -> int:
    n = len(digits)

    dp1 = [0] * n
    dp2 = [0] * n
    dp3 = [0] * n
    dp1[0] = 1

    seen_digits1 = []
    seen_digits2 = []
    seen_digits3 = []

    for _ in range(n):
        seen_digits1.append(set([]))
        seen_digits2.append(set([]))
        seen_digits3.append(set([]))

    seen_digits1[0] = {digits[0]}

    for i in range(1, len(digits)):
        d = digits[i]

        # dp1
        seen = d in seen_digits1[i - 1]
        seen_digits1[i] |= (seen_digits1[i -1] | {d})
        if seen:
            dp1[i] = dp1[i - 1]
        else:
            dp1[i] = dp1[i - 1] + 1

        # dp2
        dp2[i] = dp2[i - 1] + dp1[i - 1]
        for seen_digit1 in seen_digits1[i - 1]:
            cur_digits = 10 * seen_digit1 + d
            seen = cur_digits in seen_digits2[i - 1]
            seen_digits2[i] |= (seen_digits2[i - 1] | {cur_digits})
            if seen:
                dp2[i] -= 1

        # dp3
        dp3[i] = dp3[i - 1] + dp2[i - 1]
        for seen_digit2 in seen_digits2[i - 1]:
            cur_digits = 10 * seen_digit2 + d
            seen = cur_digits in seen_digits3[i - 1]
            seen_digits3[i] |= (seen_digits3[i - 1] | {cur_digits})
            if seen:
                dp3[i] -= 1

    return dp3[n - 1]


n = int(input().strip())
digits = [int(c) for c in input().strip()]
print(solve(digits))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

count = 0

memo = [[-1 for _ in range(10)] for _ in range(10)]

num_list = set(S)
# memo = [[-1 for _ in range(len(num_list))] for _ in range(len(num_list))]

for i in range(len(S)-3 +1):
    i_target = int(S[i])
    for j in range(i+1, len(S)-2 + 1):
        j_target = int(S[j])
        if memo[i_target][j_target] == -1:
            memo[i_target][j_target] = 1
            count += len(set(S[j+1:]))

    fin_f = True

    for x_num in num_list:
        for y_num in num_list:
            if memo[int(x_num)][int(y_num)] == -1:
                fin_f = False

    if fin_f:
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

pattern = 0

for i in range(1000):
    pin = str(i)
    while len(pin) < 3:
        pin = "0" + pin
    pin_i = 0
    for s in S:
        if s == pin[pin_i]:
            pin_i += 1
            if pin_i == 3:
                pattern += 1
                break

print(pattern)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s= input()
ans=0
for x in range(1000):
    num=str("{0:03d}".format(x))
    for i in range(n-2):
        for j in range(i+1,n-1):
            for k in range(j+1,n):
                if(num==s[i]+s[j]+s[k]):
                    ans+=1
                    break
            else:
                continue
            break
        else:
            continue
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()

d = {}

for i in range(n-2):
    for j in range(i+1, n-1):
        for l in range(j+1, n):
            r = ""
            r = s[i] + s[j] + s[l]
            if r not in d:
                d[r] = 0
            d[r] += 1

print(len(d))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_set = set()
i_set = set()

ans_list = {}


for i in range(N):
    if S[i] in i_set:
        continue
    i_set.add(S[i])
    for j in range(i+1, N):
        if not S[j + 1:N] or S[i] + S[j] in S_set:
            continue
        ans_list[S[i] + S[j]] = len(set(S[j + 1:N]))
        S_set.add(S[i] + S[j])
count = 0
# print(ans_list)
# ans_len_list = [len(set(value)) for value in ans_list.values()]
print(sum(ans_list.values()))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(map(str, input()))

def solve(k):
  k1, k2, k3 = list(k)
  ans = 0
  for i1 in range(n):
    if s[i1] == k1:
      for i2 in range(i1+1, n):
        if s[i2] == k2:
          for i3 in range(i2+1, n):
            if s[i3] == k3:
              return 1
  return 0

ans = 0
for k in range(1000):
  k = '{:0=3}'.format(k)
  ans += solve(k)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())

list=list(S)

kei=[]
for a in range(N-2):
    for b in range(N-1):
        if b<=a:
            continue
        for c in range(N):
            if c<=b:
                continue
            retsu=''
            retsu=retsu+list[a]+list[b]+list[c]
            kei.append(retsu)
last=set(kei)
print(len(last))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())

cnt = [0] * 1000
for i in range(len(s)-2):
    for j in range(i+1, len(s)-1):
        for k in range(j+1, len(s)):
            S = ''.join([s[i], s[j], s[k]])
            cnt[int(S)] += 1

ans = 0
for i in range(len(cnt)):
    if cnt[i] > 0:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_set = set()
i_set = set()

ans_list = {}


for i in range(N):
    if S[i] in i_set:
        continue
    i_set.add(S[i])
    for j in range(i+1, N):
        if not S[j + 1:N] or S[i] + S[j] in S_set:
            continue
        ans_list[S[i] + S[j]] = S[j + 1:N]
        S_set.add(S[i] + S[j])
count = 0
# print(ans_list)
ans_len_list = [len(set(value)) for value in ans_list.values()]
print(sum(ans_len_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())

ans = []
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            ans.append(S[i]+S[j]+S[k])
ans_set = set(ans)
print(len(ans_set))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())

dic = {}
for i in range(0, len(S) - 1):
    first = S[i]
    for j in range(i + 1, len(S) - 1):
        second = S[j]
        right_set = set(S[j + 1:])
        if dic.get((first, second), None) is None:
            dic[(first, second)] = right_set
        else:
            dic[(first, second)] = dic[(first, second)] | right_set

ans = 0
for key in dic.keys():
    ans += len(dic[key])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

L, R = [0] * 10, [0] * 10

L[int(S[0])] += 1
for s in S[2:]:
    R[int(s)] += 1

memo = set()
for i, s in enumerate(S[1:-1], 1):
    for l, v in enumerate(L):
        if v <= 0:
            continue
        for r, v in enumerate(R):
            if v <= 0:
                continue
            memo.add(str(l) + s + str(r))
    L[int(s)] += 1
    R[int(S[i + 1])] -= 1

print(len(memo))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
l=[]
for i in range(n):
    for j in range(i+1,n):
      #if i==j:continue
      for k in range(j+1,n):
        #if i==k or j==k:continue
        a=s[i]+s[j]+s[k]
        if a in l:continue
        l.append(a)

print(len(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

count = 0

memo = [[-1 for _ in range(10)] for _ in range(10)]

for i in range(len(S)-3 +1):
    i_target = int(S[i])
    for j in range(i+1, len(S)-2 + 1):
        j_target = int(S[j])
        if memo[i_target][j_target] == -1:
            memo[i_target][j_target] = 1
            count += len(set(S[j+1:]))

print(count)

Submission status: Time Limit Exceeded

Solution code:
def judge(candidate,S):
    for i in range(0,len(S)):
        if(S[i]==candidate[0]):
            for j in range(i+1,len(S)):
                if(S[j]==candidate[1]):
                    for k in range(j+1,len(S)):
                        if(S[k]==candidate[2]):
                            return 1
                        elif(S[k]==S[j]):
                            j=k
                elif(S[j]==S[i]):
                    i=j  
    return 0
def main():
    count=0
    N=int(input())
    S=input()
    number_dict={}
    number=[]
    for i in S:
     if i not in number_dict:
        number.append(i)
    number_list=[]
    
    
    for i in number:
        for j in number:
            for k in number:
                if (str(i)+str(j)+str(k) not in number_list):
                    number_list.append(str(i)+str(j)+str(k))

    for i in number_list:
        count+=judge(i,S)
    print(count)

main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()

result = set()

for i1 in range(N):
    for i2 in range(i1+1, N):
        for i3 in range(i2+1, N):
            result.add(S[i1] + S[i2] + S[i3])
            if len(result) >= 10**3:
                print(10**3)
                exit()

print(len(result))

Submission status: Time Limit Exceeded

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
S = read()[:-1]


def permutation_3to2(new3num: str):
    return (new3num[1:], new3num[0] + new3num[2])


passes = set()
set_2num = set()
passes.add(S[:3])
set_2num.add(S[:2])
tmp1, tmp2 = permutation_3to2(S[:3])
set_2num.add(tmp1)
set_2num.add(tmp2)
for c in S[3:]:
    for a, b in list(set_2num):
        tmp = a + b + c
        tmp1, tmp2 = permutation_3to2(tmp)
        set_2num.add(tmp1)
        set_2num.add(tmp2)
        passes.add(tmp)

print(len(passes))

Submission status: Time Limit Exceeded

Solution code:
import copy as cp
N = int(input())
S = input()
total = 0

count = [0]*10
for i in range(N):
    count[int(S[i])] += 1
    
after = [[0]*10 for i in range(N)]
post = [0]*10
for i in range(N-2,-1,-1):
    post[int(S[i+1])] += 1
    after[i] = cp.copy(post)

for i in range(1000):
    first = i//100
    second = (i-first*100)//10
    third = i-first*100-second*10
    count_i = [0]*10
    count_i[first] += 1
    count_i[second] += 1
    count_i[third] += 1
    if count_i[first] > count[first] or count_i[second] > count[second] or count_i[third] > count[third]:
        continue
    d_f = str(first)
    d_s = str(second)
    d_t = str(third)
    flag=0
    for j in range(N):
        if flag==0:
            if S[j]==d_f:
                flag=1
        elif flag==1:
            if S[j]==d_s:
                if after[j][third]>0:
                    total += 1
                    break
    if total==1000:
        break
print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_set = set()
i_set = set()

ans_list = {}


for i in range(N):
    if i in i_set:
        continue
    i_set.add(S[i])
    for j in range(i+1, N):
        if not S[j + 1:N] or S[i] + S[j] in S_set:
            continue
        ans_list[S[i] + S[j]] = S[j + 1:N]
        S_set.add(S[i] + S[j])
count = 0
# print(ans_list)
ans_len_list = [len(set(value)) for value in ans_list.values()]
print(sum(ans_len_list))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
s = tuple(map(int, input()))
f = []
ans = 0

for i in range(n-2):
    right = n - 1
    if s[i] in f:
        continue
    else:
        f.append(s[i])
        left = i + 1
        a = tuple(set(itertools.combinations(s[left:right+1], 2)))
        ans += len(a)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
S = input()
a = []
B = list(map(int, list(S)))
for i in range(N):
  for j in range(i+1, N):
    for k in range(0, 10):
      if k in set(B[j+1:]):
        a.append(S[i] + S[j] + str(k))
print(len(set(a)))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
counter=0
for i in range(1000):
  I=str(i)
  n=len(I)
  if n!=3:
    I=str(0)*(3-n)+I
  p=0
  t=-1
  ANS=1
  k=0
  for j in range(3):
    p=t+1
    while p<N:
      if S[p]==I[j]:
        t=p
        k=k+1
        break
      p=p+1
    if p==N-1 and k!=3:
      ANS=0
      break

  if  k==3:
      counter+=1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import re
args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False
input = sys.stdin.readline
N = int(input())
S = input()
count = 0
for i in range(0,1000):
  num = str(i).zfill(3)
  value=".*"+num[0]+".*"+num[1]+".*"+num[2]+".*"
  jud = re.match(r"%s" % value, S)
  if jud != None:
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

from math import factorial
import itertools

def permutations_count(n, r):
    ''' 順列 '''
    return factorial(n) // factorial(n - r)

def combinations_count(n, r):
    ''' 組み合わせ '''
    return factorial(n) // (factorial(n - r) * factorial(r))

n = int(input())
*s, = map(int, list(input().split()[0]))

ans = 0
for i in range(1000):
    num_str = str(i)
    if i < 100:
        num_str = '0'+num_str
    if i<10:
        num_str = '0'+num_str
    idx = 0
    for j in s:
        # print(j, idx)
        if j == int(num_str[idx]):
            idx += 1
            if idx==3:
                break
    if idx == 3:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
S_set = set()
i_set = set()

ans_list = {}


for i in range(N):
    if i in i_set:
        continue
    i_set.add(i)
    for j in range(i+1, N):
        if not S[j + 1:N] or S[i] + S[j] in S_set:
            continue
        ans_list[S[i] + S[j]] = S[j + 1:N]
        S_set.add(S[i] + S[j])
count = 0
# print(ans_list)
ans_len_list = [len(set(value)) for value in ans_list.values()]
print(sum(ans_len_list))

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
data = list(map(int,input()))
result = []
def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]


result = list(itertools.combinations(data, 3))

print(len(get_unique_list(result)))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
dicts=[]
ans=0
dicts=[int(S[i]+S[j]+S[k]) for i in range(0,N-2) for j in range(i+1,N-1) for k in range(j+1,N)]
print(len(set(dicts)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()

d = {}
for i in range(n-2):
    for j in range(i+1, n-1):
        for l in range(j+1, n):
            r = s[i] + s[j] + s[l]
            if r not in d:
                d[r] = 0
            d[r] += 1

print(len(d))

Submission status: Time Limit Exceeded

Solution code:
import itertools
a=input()
c=list(itertools.combinations(input(),3))
d=list(set(c))
print(len(d))