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: Accepted

Solution code:

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = True
show_flg = False


def main():
    N = int(input())
    S = input()
    pos = [[] for i in range(10)]
    ans = 0

    for i in range(len(S)):
        pos[int(S[i])].append(i)

    show(pos)

    NUM = 10
    for i in range(NUM):
        for j in range(NUM):
            for k in range(NUM):
                cur = -1
                # print(pos[k])
                pos_i = [m for m in pos[i]]

                pos_j = [m for m in pos[j]]
                if j == i and len(pos_j) > 1:
                    pos_j.pop(0)

                pos_k = [m for m in pos[k]]
                if k == i and len(pos_k) > 1:
                    pos_k.pop(0)
                if k == j and len(pos_k) > 1:
                    pos_k.pop(0)

                if len(pos_i) == 0:
                    show('len(pos_i) == 0:')
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue

                cur = pos_i.pop(0)

                #################################

                if len(pos_j) == 0:
                    show('len(pos_j) == 0:')
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue

                # prev_cur = cur
                idx = bisect.bisect_right(pos_j, cur)
                # print(cur, idx, pos_j)
                if len(pos_j) <= idx:
                    show('pos_j', idx, cur, pos_j)
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue

                if cur > pos_j[idx]:
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue
                cur = pos_j[idx]
                # show('bisect', pos_j[test], prev_cur)
                # while len(pos_j) > 0:
                #     tmp = pos_j.pop(0)
                #     if tmp > cur:
                #         cur = tmp
                #         break
                # show('while', cur, prev_cur)

                # if prev_cur == cur:
                #     show('pos_j', tmp, cur)
                #     show(i, j, k, pos[i], pos[j], pos[k])
                #     continue

                #################################

                if len(pos_k) == 0:
                    show('len(pos_k) == 0:')
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue

                idx = bisect.bisect_right(pos_k, cur)
                # print(cur, idx, pos_j)
                if len(pos_k) <= idx:
                    show('pos_k', idx, cur, pos_k)
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue

                if cur > pos_k[idx]:
                    show(i, j, k, pos[i], pos[j], pos[k])
                    continue
                cur = pos_k[idx]
                # prev_cur = cur
                # while len(pos_k) > 0:
                #     tmp = pos_k.pop(0)
                #     if tmp > cur:
                #         cur = tmp
                #         break

                # if prev_cur == cur:
                #     show('pos_k', tmp, cur)
                #     show(i, j, k, pos[i], pos[j], pos[k])
                #     continue

                # show(i, j, k)
                ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
import bisect

N = int(input())
S = input()
dst = [[] for i in range(10)]
for i,x in enumerate(S):
    y = int(x)
    dst[y].append(i)

bs = bisect.bisect_right
ans = 0

for k in range(10):
    for j in range(10):
        for i in range(10):
            if dst[i] == [] or dst[j] == [] or dst[k] == []:
                continue
            else:
                A = dst[i][0]
                if A >= dst[j][-1]:
                    continue
                else:
                    b = bs(dst[j],A)
                    B = dst[j][b]
                    if B >= dst[k][-1]:
                        continue
                    else:
                        ans += 1            
            
print(ans)

Submission status: Accepted

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

next = [[-1] * 10 for _ in range(N + 1)]
for i in range(N - 1, -1, -1):
    for j in range(10):
        next[i][j] = next[i + 1][j]
    next[i][S[i]] = i

c = 0
for i in range(10):
    p0 = next[0][i]
    if p0 < 0 or p0 > N - 3:
        continue
    for j in range(10):
        p1 = next[p0 + 1][j]
        if p1 < 0 or p1 > N - 2:
            continue
        for k in range(10):
            p2 = next[p1 + 1][k]
            if p2 > 0:
                c += 1
print(c)

Submission status: Accepted

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

a=[]
b=[]
c=[]
m=0
for i in range(n-2):
    if l[i] in a:
        continue
    a.append(l[i])
    for j in range(i+1,n-1):
        if l[j] in b:
            continue
        b.append(l[j])
        for k in range(j+1,n):
            if l[k] in c:
                continue
            c.append(l[k])
            m+=1
        c=[]
    b=[]

print(m)

Submission status: Accepted

Solution code:
def count_2chr(li):
    ans = [0 for _ in range(10)]
    s = li.copy()
    while len(s) >= 2:
        i = s.pop(0)
        if ans[i] == 0:
            ans[i] = len(set(s))
    
    return sum(ans)

n = int(input())
s = list(map(int, list(input())))

count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

while len(s) >= 3:
    i = s.pop(0)
    if count[i] == 0:
        count[i] = count_2chr(s)

print(sum(count))

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    S=[int(x) for x in list(s)]
    Pass1={S[0],S[1],S[2]}
    Pass2={S[0]*10+S[1],S[0]*10+S[2],S[1]*10+S[2]}
    Pass3={S[0]*100+S[1]*10+S[2]}
    for k in S[3:]:
        for i in Pass2:
            Pass3.add(i*10+k)
        for i in Pass1:
            Pass2.add(i*10+k)
        Pass1.add(k)
    print(len(Pass3))
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:

n = int(input())
s = input()

imemo = [0]*10
memo = [0]*100
counter = 0

for i in range(n):
    if imemo[int(s[i])] == 0:
        imemo[int(s[i])] = 1
    else:
        continue
        
    for j in range(i+1,n):
        if memo[int(s[i])*10+int(s[j])] == 0:
                memo[int(s[i])*10+int(s[j])] = 1
                counter += len(set(s[j+1:]))
print(counter)

Submission status: Accepted

Solution code:
def can_set(num, string):
    d0, d1, d2 = num

    ind0 = string.find(d0)
    if ind0 == -1 or ind0 == len(string) - 1:
        return False

    string = string[ind0 + 1:]

    ind1 = string.find(d1)
    if ind1 == -1 or ind1 == len(string) - 1:
        return False

    string = string[ind1 + 1:]

    ind2 = string.find(d2)

    if ind2 == -1:
        return False

    return True


n = int(input())
s = input().strip()

count = 0
for i in range(1000):
    num = str(i).zfill(3)
    if can_set(num, s):
        count += 1

print(count)

Submission status: Accepted

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

a = dict()

for i in range(N):
    s = S[i]
    for j in range(100):
        if ("0" + str(j))[-2:] in a:
            a[("0" + str(j) + s)[-3:]] = 1
    if (N - i) >= 2:
        for j in range(10):
            if str(j) in a:
                a[str(j) + s] = 1
    if (N - i) >= 3:
        a[str(s)] = 1

ans = 0
for k in a.keys():
    if len(k) == 3:
        ans += 1
print(ans)

Submission status: Accepted

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

numbers = []

for i in range(0, 10):
    for j in range(0, 10):
        for k in range(0, 10):
            numbers.append( str(i) + str(j) + str(k))

result = 0
for number in numbers:
    first = number[0]
    second = number[1]
    third = number[2]

    if first in S_list:
        first_index = S_list.index(first)

        if second in S_list[(first_index+1):]:
            second_index = (S_list[(first_index+1):]).index(second) + first_index + 1

            if third in S_list[(second_index+1):]:
                result += 1

    else:
        continue

print(result)

Submission status: Accepted

Solution code:
n = int(input())
code = input()
f = 0
total = 0
for i in range(0,1000):
  s = str(i)
  if i< 10:
    s = '00' + s
  elif i < 100:
    s = '0' + s
  idx = -1
  for dig in range(3):
    idx = code.find(s[dig], idx+1) 
    if idx == -1:
      break
  if idx != -1:
    total += 1
print(total)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
S = [int(s) for s in S]

left = [[0]*10 for _ in range(N)]
right = [[0]*10 for _ in range(N+1)]

ans = [0]*1000 #ans[000]~[999]=Bool

for i,s in enumerate(S):
  for j in range(10):
    left[i][j] = left[i-1][j]
  left[i][s] = 1
  
for i,s in enumerate(S[::-1]):
  for j in range(10):
    right[N-i-1][j] = right[N-i][j]
  right[N-i-1][s] = 1
  
for i,s in enumerate(S):
  if 0<i<N-1:
    for l in range(10):
      for r in range(10):
        if left[i-1][l] and right[i+1][r]:
          #print(100*l+10*s+r)
          ans[100*l+10*s+r]=1
          
print(sum(ans))

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = input()
#反対から種類を記録
#ls = []
#for i in range(n-1,-1,-1):
#    if s[i] not in ls:
#        ls.append(i)
dic_forward = {}
for i in range(n-2):
    if int(s[i]) not in dic_forward.keys():
        #print(i,s[i])
        dic_forward[int(s[i])] = i

dic_backward = {}
for i in range(n-1,1,-1):
    if int(s[i]) not in dic_backward.keys():
        dic_backward[int(s[i])] = i

for i in range(10):
    if i not in dic_forward.keys():
        dic_forward[i] = -1
for i in range(10):
    if i not in dic_backward.keys():
        dic_backward[i] = -1

#print(dic_forward)
#print(dic_backward)
ans = 0

for i in range(10):
    for j in range(10):
        if dic_forward[i] >= 0 and dic_backward[j] >= 0:
            if dic_backward[j] - dic_forward[i] > 1:
                #print(i,j)
                #print(collections.Counter(s[dic_forward[i]+1:dic_backward[j]]))
                #print(len(collections.Counter(s[dic_forward[i]+1:dic_backward[j]])))
                ans += len(collections.Counter(s[dic_forward[i]+1:dic_backward[j]]))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

x=int(input())
n=str(input())


#print(countN)
#abc
aaa=set([])
result=0
for i in range(x-2):
    a=n[i]
    if a not in aaa:
        aaa.add(a)
        bbb=set([])
        for j in range(i+1,x-1):
            b=n[j]
            if b not in bbb:
                bbb.add(b)
                ccc=set([])
                for k in range(j+1,x):
                    c=n[k]
                    if c not in ccc:
                        ccc.add(c)
                        result+=1
    if len(aaa)==10:
        break
print(result)

Submission status: Accepted

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

kind = 0

first = []
for i in range(n-2):
  if s[i] not in first:
    first.append(s[i])
    second = []
    for j in range(i+1, n-1):
      if s[j] not in second:
        second.append(s[j])
        third = []
        for k in range(j+1, n):
          if s[k] not in third:
            third.append(s[k])
        kind += len(third)
print(kind)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
s=input()
t=[[] for i in range(10)]
for i in range(n):
    t[int(s[i])].append(i)
c=0
for i in range(10):
    if len(t[i])==0:
        continue
    for j in range(10):
        if len(t[j]) == 0:
            continue
        m=bisect.bisect(t[j],t[i][0])
        if len(t[j])==m:
            continue
        for k in range(10):
            if len(t[k]) == 0:
                continue
            n=bisect.bisect(t[k],t[j][m])
            if len(t[k])==n:
                continue
            else:
                c+=1
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
s = list(map(int, input()))

a = [0] * n
tmp = set()
for i in range(n)[::-1]:
    a[i] = len(tmp)
    tmp.add(s[i])

b = [0] * n
tmp = defaultdict(int)
for i in range(n)[::-1]:
    for x in tmp.values():
        b[i] += a[x]
    tmp[s[i]] = i

ans = 0
tmp = set()
for i in range(n):
    if s[i] in tmp: continue
    ans += b[i]
    tmp.add(s[i])

print(ans)

Submission status: Accepted

Solution code:
def examD():
    N = I();  S = SI()
    dp = [[0]*10 for _ in range(N+1)]
    for i in range(N):
        for j in range(10):
            dp[i+1][j] = dp[i][j]
        dp[i+1][int(S[i])] +=1
    left = [-1]*10; right = [-1]*10
    for i in range(N):
        if left[int(S[i])]==-1:
            left[int(S[i])] = i
    for i in range(N-1,-1,-1):
        if right[int(S[i])]==-1:
            right[int(S[i])] = i
    ans = 0
#    print(left)
#    print(right)
    for i in left:
        if i>=0:
            for j in right:
                if j>=0:
                    for k in range(10):
                        if dp[j][k]>0 and dp[j][k] - dp[i+1][k] > 0:
                            ans += 1
#                            print(S[i],k,S[j])
    print(ans)
    return
def examE(mod):
    N = I()
    A = LI()
    a =[0,0,0]
    cur = 1
    for i in A:
        if a[0]==i:
            if a[0]==a[1]:
                if a[1]==a[2]:
                    cur *= 3
                else:
                    cur *=2
            a[0]+=1
        elif a[1]==i:
            if a[1] == a[2]:
                cur *= 2
            a[1] +=1
        elif a[2]==i:
            a[2]+=1
        else:
            cur *=0
        a.sort(reverse=True)
        cur %=mod
    print(cur)
    return

def examF():
    T = LI()
    A = LI()
    B = LI()
    loop1 = T[0]*(A[0]-B[0])
    loop2 = T[1]*(A[1]-B[1])
    if loop1>0 and loop2>0:
        ans = 0
    elif loop1<0 and loop2<0:
        ans = 0
    else:
        now = loop1
        cur = loop1+loop2
#        print(now,cur)
        if cur!=0:
            ans = abs(now)//abs(cur)*2
            if abs(now)%abs(cur)!=0:
                judge = now + cur*ans//2
                if (now>0 and judge+loop1>0) or (now<0 and judge+loop1<0):
                    ans +=1
        else:
            ans = "infinity"
    print(ans)
    return

import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
s_rev = s[::-1]

ans = 1000

can = [i for i in range(0,1000)]
can_str = []

for c in can:
    can_s = str(c)
    if len(can_s) == 1:
        can_s = '00' + can_s
    elif len(can_s) == 2:
        can_s = '0' + can_s
    can_str.append(can_s)

for c in can_str:
    try:
        i = s.index(c[0])
        k = s_rev.index(c[2])
        ind = n - k -1
        if i >= ind:
            raise ValueError
        if s[i+1:ind].count(c[1]) == 0:
            raise ValueError
    except:
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right as br

def main():
    N = int(input())
    S = input()
    P = [[ ] for _ in range(10)]
    for s,index in zip(S,range(N)):
        P[int(s)].append(index)
    ans = 0 
    for i in range(0,1000):
        
        a = i // 100
        b = (i % 100) // 10
        c = i % 10
        try:
            if len(P[a]) > 0:
                point = P[a][0]
                if len(P[b]) > 0:
                    tmp = br(P[b],point)
                    if P[b][tmp] > point:
                        point = P[b][tmp]
                        if len(P[c]) > 0:
                            neko = br(P[c],point)
                            if P[c][neko] > point:
                                ans += 1
        except:
            ans = ans
    print(ans)
    return 

main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s2 = [int(v) for v in s]

cnt = 0

for i in range(0, 10):
    if i in s2:
        ad1 = s.find(str(i))
        for j in range(0, 10):
            if j in s2[ad1 + 1:]:
                ad2 = s.find(str(j), ad1 + 1)
                for k in range(0, 10):
                    if k in s2[ad2 + 1:]:
                        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(10):
    for j in range(10):
        for k in range(10):
            flag=[0,0,0]
            for index in range(n):
                if flag[0]==0 and flag[1]==0 and  s[index]==str(i):
                    flag[0]=1
                elif flag[0]==1 and flag[1]==0 and s[index]==str(j):
                    flag[1]=1
                elif flag[0]==1 and flag[1]==1 and s[index]==str(k):
                    ans+=1
#                     print(i,j,k)
                    break
                else:
                    continue
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(1000):
  i1 = str(i)
  i2 = i1.zfill(3)
  index1 = s.find(i2[0])
  if (index1 == -1) or (index1 == n-1):
    continue
  else:
    index1 += 1
    index2 = s.find(i2[1],index1)
    if (index2 == -1) or (index2 == n-1):
      continue
    else:
      index2 += 1
      index3 = s.find(i2[2],index2)
      if index3 != -1:
        ans += 1
print(ans)

Submission status: Accepted

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

for i in range(1000):
  num = '{:03}'.format(i)
  num_i = 0
  for j in range(n):
    if num[num_i] == s[j]:
      num_i += 1
      if num_i > 2:
        # 生成可能
        ans += 1
        break

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

N=int(input())
S=input()
pos=[[] for _ in range(10)]
ans=0
for i in range(N):
  pos[int(S[i])].append(i)
for i in range(10**3):
  digit1, digit2, digit3=i//100, (i%100)//10, i%10
  if not pos[digit1] or not pos[digit2] or not pos[digit3]:
    continue
  pos1=pos[digit1][0]
  if pos1>=pos[digit2][-1]:
    continue
  idx2=bisect_right(pos[digit2], pos1)
  if idx2>=len(pos[digit2]):
    continue
  pos2=pos[digit2][idx2]
  if pos2>=pos[digit3][-1]:
    continue
  idx3=bisect_right(pos[digit3], pos2)
  if idx3>len(pos[digit3]):
    continue
  else:
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

nums = {i:[] for i in range(10)}

for idx, s in enumerate(S):
    nums[int(s)].append(idx+1)
    
length = {i:len(nums[i]) for i in range(10)}
ans = 0
for i in range(1000):
    idx = 0   
    
    for j in str(i).zfill(3):
        n = int(j)
        x = bisect_right(nums[n], idx)
        if length[n] == x: break
        idx = nums[n][x]
    else: 
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = str(input())

ans = 0
for i in range(10):
  i_bannme = s.find(str(i))
  if i_bannme == -1:
    continue
  else:
    for j in range(10):
      j_bannme = s.find(str(j), i_bannme+1)
      if j_bannme== -1:
        continue
      else:
        ans += len(collections.Counter(s[j_bannme+1:]))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
dp1 = {}
for i in range(10):
    dp1[str(i)] = -1
dp2 = {}
for i in range(10):
    for j in range(10):
        dp2[str(i) + str(j)] = -1

for i in range(n):
    if dp1[s[i]] == -1:
        dp1[s[i]] = i

for i in range(10):
    if dp1[str(i)] != -1:
        for j in range(dp1[str(i)]+1, n):
            if dp2[str(i)+str(s[j])] == -1:
                dp2[str(i)+str(s[j])] = j

dp3 = {}
for i in range(10):
    dp3[i] = [0] * (n+1)
for i in range(n):
    dp3[int(s[i])][i+1] = 1
for i in range(10):
    for j in range(n):
        dp3[i][j+1] += dp3[i][j]


ans = 0
for i in range(10):
    for j in range(10):
        t = dp2[str(i)+str(j)]
        for k in range(10):
            if t != -1: 
                if dp3[k][n] - dp3[k][t+1] >= 1:
                     ans += 1
print(ans)

Submission status: Accepted

Solution code:
input()
S=list(input())
ans=0

def index(x,l):
    if x in l:
        return l.index(x)
    else:
        return -1

for i in range(1000):
    Si=0
    s=list(str(i).zfill(3))
    si=0
    if index(s[0],S) != -1:
        if index(s[1],S[S.index(s[0])+1:]) != -1:
            if index(s[2],S[S.index(s[0])+1:][S[S.index(s[0])+1:].index(s[1])+1:]) != -1:
                ans += 1
print(ans)

Submission status: Accepted

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

count = 0
for i in range(10):
  n1 = S.find(str(i))
  if not(n1 == -1 or n1 >= N-2):
    S2 = S[n1+1:]
    for j in range(10):
      n2 = S2.find(str(j))
      if not(n2 == -1 or n2 >= N-1):
        S3 = S2[n2+1:]
        for k in range(10):
          n3 = S3.find(str(k))
          if(n3 != -1):
            count += 1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
list_s = [[] for _ in range(10)]
exist = []
a_c = []
count = 0

# 数字毎にindexをまとめる
for index, num_str in enumerate(s):
    list_s[int(num_str)].append(index)

# 存在しない数字を省く
for i in range(10):
    if list_s[i]:
        exist.append(i)

# 最初と最後の桁の候補
for a in exist:
    for c in exist:
        a_c.append((a, c))

for b in exist:  # bを決めて
    for a, c in a_c:  # a,cを決めて
        for b_index in list_s[b]:  # bのindexをあたる
            if list_s[a][0] < b_index < list_s[c][-1]:
                count += 1
                break

print(count)

Submission status: Accepted

Solution code:
words = lambda t : list(map(t, input().split()))
n = int(input())
a = input()

d = {}
r_nums = [0] * 10
for i in range(1,n):
    r_nums[int(a[i])] += 1
l_nums = [0] * 10

for i in range(1,n-1):
    center = int(a[i])
    r_nums[int(a[i])] -= 1
    l_nums[int(a[i-1])] += 1
    #print(l_nums, center, r_nums)
    for j in range(10):
        for k in range(10):
            if r_nums[j] > 0 and l_nums[k] > 0:
                d[k*100 + center*10 + j] = 1
    #print(d)
print(len(d))

Submission status: Accepted

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

pins = []
for i in range(10):
  for j in range(10):
    for k in range(10):
      pins.append(str(i) + str(j) + str(k))

ans = 0
for p in pins:
  i = S.find(p[0])
  if i >= 0:
    j = S[i + 1 :].find(p[1])
    if j >= 0:
      k = S[i + j + 2 :].find(p[2])
      if k >= 0:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
count = 0
for i in range(1000):
    j = i
    if(j<10):
        first = S.find('0')    
        if(first >= 0 and first < N):
            second = S[first+1:].find('0')    
            if(second >= 0 and first+second+2 < N):
                third = S[first+second+2:].find(str(j))
                if(third>=0):
                    count += 1
    elif(j<100):
        first = S.find('0')
        if(first >= 0 and first < N):
            second = S[first+1:].find(str((j%100)//10))
            if(second >= 0 and first+second+2 < N):
                third = S[first+second+2:].find(str(j%10))
                if(third>=0):
                    count += 1
    else:
        first = S.find(str(j//100))    
        if(first >= 0 and first < N):
            second = S[first+1:].find(str((j%100)//10))    
            if(second >= 0 and first+second+2 < N):
                third = S[first+second+2:].find(str(j%10))
                if(third>=0 ):
                    count += 1  
print(count)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    S = readline()
    S = S.decode("UTF-8")
    S = list(S[0:-1])

    cnt = 0
    for i in range(10):
        if str(i) in S:
            ind_i = S.index(str(i))
        else:
            continue
        for j in range(10):
            if str(j) in S[ind_i + 1:]:
                ind_j = S[ind_i + 1:].index(str(j))
                ind_j += ind_i + 1
            else:
                continue
            for k in range(10):
                if str(k) in S[ind_j + 1:]:
                    cnt += 1
                else:
                    continue

    print(cnt)


main()

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

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

ans = 0
for i in range(1000):
    nums = list(str(i).zfill(3))
    fir = 0
    cnt = 0
    for num in nums:
        try:
            fir = S[fir:].index(num) + fir + 1
            cnt += 1
        except:
            break
    if cnt == 3:
        # print(str(i).zfill(3))
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=list(input())
L=[str(i)+str(j) for i in range(10) for j in range(10)]
n=0
for l in L:
  I=N
  for i in range(N):
    if S[i]==l[0]:
      I=i
      break
  J=N
  for j in range(I+1,N):
    if S[j]==l[1]:
      J=j
      break
  n+=len(list(set(S[J+1:N])))
print(n)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
NEX = [[float("inf")]*10 for i in range(N+1)]
for i in range(N):
    j = N-1-i
    for k in range(10):
        NEX[j][k] = NEX[j+1][k]
    NEX[j][int(S[j])] = j
from collections import defaultdict
dp = defaultdict(int)
for i in range(1000):
    j = "0"*(3-len(str(i))) + str(i)
    dp[i] = 0
ans = 0    
for i in range(10):
    idx1 = NEX[0][i]
    if idx1 != float("inf"):
        for j in range(10):
            idx2 = NEX[idx1+1][j]
            if idx2 != float("inf"):
                for k in range(10):
                    idx3 = NEX[idx2+1][k]
                    if idx3 != float("inf"):
                        ans += 1
                        #print(str(i)+str(j)+str(k))
print(ans)

Submission status: Accepted

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

    count = 0

    for i in range(10):
        a = S.find(str(i))
        if a == -1:
            continue

        for j in range(10):
            b = S[a+1:].find(str(j))
            if b == -1:
                continue

            for k in range(10):
                if str(k) in S[a+b+2:]:
                    # print(i*100+j*10+k)
                    count += 1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

def main():
    N = int(input())
    S = input()

    fst_num = []
    scd_num = []
    count = 0
    for i in range(len(S)-2):
        if len(fst_num) == 10:
            break
        if S[i] not in fst_num:
            fst_num.append(S[i])
            scd_num = []
            for j in range(i+1, len(S) - 1):
                if len(scd_num) == 10:
                    break
                if S[j] not in scd_num:
                    scd_num.append(S[j])
                    for k in range(10):
                        if str(k) in S[j+1:]:
                            # print(S[i],S[j],k)
                            count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

memo=[0]*1000

sudeni1=set() #その時点で登場した数の配列をまず用意
sudeni2=set() #その時点で作れる2桁の数を入れた配列(01とかも含む)
sudeni3=set()

sudeni1.add(str(s[0]))
sudeni1.add(str(s[1]))
sudeni2.add(str(s[0])+str(s[1]))

for i in range(2,n):
  temp1=list(sudeni1)
  temp2=list(sudeni2)
  for j in temp2: #その桁までで作れる3桁の数に追加。
    sudeni3.add(j+str(s[i]))
  for j in temp1: #その桁までで作れる2桁の数に追加。
    sudeni2.add(j+str(s[i]))
  sudeni1.add(str(s[i]))

print(len(sudeni3))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
L1=[0]*10

count=0
for idx1 in range(len(S) -2):
    C1 = S[idx1]
    if L1[int(C1)] == 1:
        continue
    L1[int(C1)] = 1
    L2=[0]*10
    for idx2 in range(len(S) -1)[idx1 + 1:]:
        C2 = S[idx2]
        if L2[int(C2)] == 1:
            continue
        L2[int(C2)] = 1
        L3=[0]*10
        for idx3 in range(len(S))[idx2 + 1:]:
            C3 = S[idx3]
            if L3[int(C3)] == 1:
                continue            
            L3[int(C3)] = 1
            count += 1
print(count)

Submission status: Accepted

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


pins = set()
pins_check = set()

def bfs():    
    from collections import deque
    queue = deque([(S, '', 0)])
    while queue:
        tmp_s, tmp_pin, tmp_cnt = queue.popleft()
        if tmp_cnt == 3:
            pins.add(tmp_pin)
            continue
        
        for i in range(len(tmp_s)):
            if tmp_pin+tmp_s[i] in pins_check:
                continue
            pins_check.add(tmp_pin+tmp_s[i])
            queue.append((tmp_s[i+1:], tmp_pin+tmp_s[i], tmp_cnt+1))

ans = bfs()
#print(pins)
print(len(pins))

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
s=[int(i) for i in s]
res=[[0 for i in range(10)]for i in range(n)]
for i in range(n-1,0,-1):
    for j in range(10):
        res[i-1][j]=res[i][j]
    res[i-1][s[i]]+=1
cnt=0
#print(res)
a=[0]*10
for i in range(n-2):
    if a[s[i]]!=0:
        continue
    a[s[i]]+=1
    b=[0]*10
    for j in range(i+1,n-1):
        if b[s[j]]==0:
            b[s[j]]+=1
            cnt+=10-res[j].count(0)

print(cnt)