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 sys
input = sys.stdin.readline
import copy

N=int(input())
S=input().strip()

SUB={""}
ANS=set()

for s in S:
    NSUB=copy.deepcopy(SUB)
    for t in SUB:
        x=t+s
        if len(x)<=2:
            NSUB.add(x)
        else:
            ANS.add(x)

    SUB=NSUB

print(len(ANS))

Submission status: Accepted

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

# 3桁の番号を作るにあたり、文字列から抽出して作るのではなく、000〜999までの組み合わせが存在するかを確認する。
ans = 0
for i in range(10):
  a = s.find(str(i))
  for j in range(10):
    b = s.find(str(j), a+1) # aの取得位置の次から検索を行う
    for k in range(10):
      c = s.find(str(k), b+1) # bの取得位置の次から検索を行う
      if a == -1 or b == -1 or c == -1: continue
      else: ans += 1
print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(1000):
    pin = '%03d' % i
    try:
        i0 = s.index(pin[0])
        i1 = i0 + 1 + s[i0+1:].index(pin[1])
        i2 = i1 + 1 + s[i1+1:].index(pin[2])
        cnt += 1
    except ValueError:
        pass

print(cnt)

Submission status: Accepted

Solution code:
from itertools import product

N = int(input())
S = input()
pins = list(product([i for i in range(10)], repeat=3))
ans = 0
for pin in pins:
  i = 0
  for s in S:
    if pin[i] == int(s):
      i += 1
    if i == 3:
      ans += 1
      break
print(ans)

Submission status: Accepted

Solution code:
# coding:UTF-8
import sys
from math import factorial

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

N = int(input())    # 数字
S = input()     # 文字列

res = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            src = i
            num = 0
            for s in S:
                if int(s) == src:
                    if num == 0:
                        src = j
                        num = 1
                    elif num == 1:
                        src = k
                        num = 2
                    elif num == 2:
                        res += 1
                        break

print("{}".format(res))

Submission status: Accepted

Solution code:
# D - Lucky PIN
N = int(input())
S = input()

cnt = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            a = S.find(str(i))
            if a == -1:
                continue
            b = S.find(str(j),a+1)
            if b == -1:
                continue
            c = S.find(str(k),b+1)
            if c == -1:
                continue
            cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
d = {}
k1 = [0 for i in range(10)]
for i in range(N-2):
    if k1[int(S[i])] != 0: continue
    k1[int(S[i])] += 1
    k2 = [0 for i in range(10)]
    for j in range(i+1, N-1):
        if k2[int(S[j])] != 0: continue
        k2[int(S[j])] += 1
        for k in range(j+1, N):
            st = ''.join([S[i], S[j], S[k]])
            d[st] = True
print(len(d))

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
num=list(range(10))
for i in range(10):
    num[i]=str(num[i])
ans=0
for i in range(n-2):
    if len(num)==0:
        break
    if s[i] in num:
        num2=[str(_) for _ in range(10)]
        num.remove(s[i])
        for j in range(i+1,n):
            if len(num2)==0:
                break
            if s[j] in num2:
                num2.remove(s[j])
                ans+=len(set(s[j+1:]))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(10):
  a = s.find(str(i))
  for j in range(10):
    b = s.find(str(j), a+1)
    for k in range(10):
      c = s.find(str(k), b+1)
      if a == -1 or b == -1 or c == -1:
        continue
      else:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(10):
  idx1=s.find(str(i))
  for j in range(10):
    idx2=s.find(str(j),idx1+1,n)
    for k in range(10):
      idx3=s.find(str(k),idx2+1,n)
      if idx1==-1 or idx2==-1 or idx3==-1:
        continue
      else:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
num = []
for _ in range(10):
    num.append([])

for i in range(N):
    num[int(S[i])].append(i)

ans = 0
for X1 in range(0, 10):
    for X2 in range(0, 10):
        for X3 in range(0, 10):
            X = str(X1) + str(X2) + str(X3)
            if num[X1] != [] and num[X2] != [] and num[X3] != []:
                if num[X1][0] < num[X2][-1]:
                    for i in num[X2]:
                        if num[X1][0] < i < num[X3][-1]:
                            ans += 1
                            break

print(ans)

Submission status: Accepted

Solution code:
# import numpy as np
import sys, math, heapq
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

cnt = 0
for num in range(1000):
    snum = str(num).zfill(3)
    # print("snum", snum)
    ind = 0
    chkind = 0
    # while ind < len(S):
    for s in S:
        if chkind >= 3:
            break
        if s == snum[chkind]:
            chkind += 1
    if chkind == 3:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(10):
    if str(i) in s[:-2]:
        s1=s.index(str(i))

        for j in range(10):
            if str(j) in s[s1+1:-1]:
                s2=s[s1+1:].index(str(j))+s1+1

                for k in range(10):
                    if str(k) in s[s2+1:]:
                        ans+=1

print(ans)

Submission status: Accepted

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

cnt = 0

for i in range(10):
    if i not in num_list:
        continue
    else:
        first_pwd = num_list.index(i)
        new_num_list = num_list[first_pwd + 1:]
        for j in range(10):
            if j not in new_num_list:
                continue
            else:
                second_pwd = new_num_list.index(j)
                new_new_num_list = new_num_list[second_pwd+1:]
                for k in range(10):
                    if k not in new_new_num_list:
                        continue
                    else:
                        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
num = input()
 
password_list = []
initial_list = []
initial_list2 = []
initial_list3 = []
 
for i in range(len(num)-2):
    initial_list2 = []
    if num[i] in initial_list:
        continue
    elif len(initial_list) == 10:
        break
    else:
        initial_list.append(num[i])
        for j in range(i+1,len(num)-1):
            initial_list3 = []
            if num[j] in initial_list2:
                continue
            elif len(initial_list2) == 10:
                break
            else:
                initial_list2.append(num[j])
                for k in range(j+1,len(num)):
                    if num[k] in initial_list3:
                        continue
                    elif len(initial_list3) == 10:
                        break
                    else:
                        initial_list3.append(num[k])
                        target = num[i] + num[j] + num[k]
                        if target not in password_list:
                            password_list.append(target)
                            
print(len(password_list))

Submission status: Accepted

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

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

output=0
for i in range(n):
	tmps=s[i]
	tmplist=[]
	
	for j,x in enumerate(all):
		if x[0] == tmps:
			if len(x)==1:
				tmplist.append(x)
				output += 1
			else:
				all[j] = x[1:]
	
	for tmp in tmplist:
		all.remove(tmp)

print(output)

Submission status: Accepted

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

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

ans = 0
for i in range(1000):
    pw = list("{0:03d}".format(i))

    if pw[0] in S:
        w1 = S.index(pw[0])
        if pw[1] in S[w1+1:]:
            w2 = w1+1 + S[w1+1:].index(pw[1])
            if pw[2] in S[w2+1:]:
                ans += 1

print(ans)

Submission status: Accepted

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

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

ans = 0
for i in range(1000):
    pw = list("{0:03d}".format(i))

    if pw[0] in S:
        w1 = S.index(pw[0])
        if pw[1] in S[w1+1:]:
            w2 = w1+1 + S[w1+1:].index(pw[1])
            if pw[2] in S[w2+1:]:
                ans += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(1000):
  i='0'*(3-len(str(i)))+str(i)
  if i[0] in s:
    if i[1] in s[s.index(i[0])+1:]:
      if i[2]in s[s.index(i[0])+s[s.index(i[0])+1:].index(i[1])+2:]:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(list)
for i, s in enumerate(S):
    d[s].append(i)
ans = 0

for i in range(1000):
    T = '%03d' % i
    j = -1
    for t in T:
        flag = True
        for k in d[t]:
            if k > j:
                flag = False
                j = k
                break
        if flag:
            break
    if not(flag):
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
#from collections import defaultdict
def main():
    n = int(input())
    s = tuple(map(int, tuple(input())))
    num_pos = [] # 各数字がｓ中に最後にあらわれる位置。
    for num in range(10):
        if num in s:
            num_pos.append(n - s[::-1].index(num) - 1)
    unique_num = [0] * n # ｓの各桁でその桁以降にある数字の種類。
    for i1 in range(n):
        unique_num[i1] = sum([np >= i1 for np in num_pos])
    unique_num.append(0)
    r = 0
    for keta1 in range(10):
        if keta1 in s:
            keta1_pos = s.index(keta1)
            for keta2 in range(10):
                if keta2 in s[keta1_pos + 1:]:
                    keta2_pos = s[keta1_pos + 1:].index(keta2) + keta1_pos + 1
                    r += unique_num[keta2_pos + 1]
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = 0
for i in range(10):
    a = s.find(str(i))
    if a == -1:
        continue
    for j in range(10):
        b = s.find(str(j), a + 1)
        if b == -1:
            continue
        for k in range(10):
            c = s.find(str(k), b + 1)
            if c != -1:
                cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans=0
for i in range(1000):
 l=str(i).zfill(3);k=0
 for j in range(N):
  if S[j]==l[k]:
   k+=1
   if k==3:ans+=1;break
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=list(input())
S=[0]*N
for i in range(N):
  S[i]=int(s[i])
ct=0
for i in range(10):
  if not i in S:
    continue
  T=S[S.index(i)+1:]
  for j in range(10):
    if not j in T:
      continue
    U=T[T.index(j)+1:]
    for k in range(10):
      if k in U:
        ct+=1
print(ct)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    T = tuple(map(int, input()))
    ans = 0
    for i in range(0, 1000):
        a = i // 100
        b = (i // 10) % 10
        c = i % 10
        flag1 = False
        flag2 = False
        flag3 = False
        for t in T:
            if flag2:
                if t == c:
                    flag3 = True
                    break
            if flag1:
                if t == b:
                    flag2 = True
                    continue
            if t == a:
                flag1 = True

        if flag3:
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

for i in range(10**3):
    i=str(i).zfill(3)
    if my_index(s,i[0])!=-1:
        s2=s[my_index(s,i[0])+1:]
        if my_index(s2,i[1])!=-1:
            s3=s2[my_index(s2,i[1])+1:]
            if my_index(s3,i[2])!=-1:
                ans+=1
print(ans)

Submission status: Accepted

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

num = '0123456789'
cnt = 0
for i in num:
    for j in num:
        for k in num:
            if i in s:
                idx1 = s.find(i)
                if j in s[idx1 +1 :]:
                    idx2 = s.find(j, idx1 + 1)
                    if k in s[idx2 + 1:]:
                        idx3 = s.find(k, idx2 + 1)
                        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
def getval():
    n = int(input())
    s = input()
    return n,s 

def main(n,s):
    pos = [-1 for i in range(10)]
    for i in range(n):
        temp = int(s[i])
        if pos[temp]==-1:
            pos[temp] = i 
    flag1 = [[[False for i in range(10)] for i in range(10)] for i in range(10)]
    flag2 = [[False for i in range(10)] for i in range(10)]
    for i in range(10):
        if pos[i]==-1 or pos[i]>=n-1:
            continue
        for j in range(pos[i]+1,n):
            f = flag2[i]
            temp = int(s[j])
            for k in range(10):
                if f[k]:
                    flag1[i][k][temp] = True 
            if not f[temp]:
                flag2[i][temp] = True
    ans = 0
    for i in range(10):
        for j in range(10):
            for k in range(10):
                if flag1[i][j][k]:
                    ans += 1
    print(ans)

if __name__=="__main__":
    n,s = getval()
    main(n,s)

Submission status: Accepted

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

ans = 0
for i in range(1000):
    nows = S
    istr = str(i).zfill(3)
    idx = nows.find(istr[0])
    if idx == -1:  continue
    nows = nows[idx+1:]
    idx = nows.find(istr[1])
    if idx == -1:  continue
    nows = nows[idx+1:]
    idx = nows.find(istr[2])
    if idx == -1:  continue
    ans += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = []
for i in range(1000):
    s = str(i).zfill(3)
    pos = 0
    is_exist = True
    for j in range(3):
        if s[j] in lst[pos:]:
            pos += lst[pos:].index(s[j]) + 1
        else:
            is_exist = False
            break
    if is_exist:
        ans.append(s)
print(len(ans))

Submission status: Accepted

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

ans = {}
for i in range(1000):
    s = str(i).zfill(3)
    pos = 0
    is_exist = True
    for j in range(3):
        if s[j] in lst[pos:]:
            pos += lst[pos:].index(s[j]) + 1
        else:
            is_exist = False
            break
    if is_exist:
        ans[s] = 0
print(len(ans))

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 1
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

n = k()
s = v()

s1,s2,s3 = set(),set(),set()
for x in s:
    for z in s2:
        s3.add(z+x)
    for y in s1:
        s2.add(y+x)
    s1.add(x)
print(len(s3))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
se=[set() for _ in range(3)]
for a in s:
  for pre in se[1]:
    se[2].add(a+pre)
  for pre in se[0]:
    se[1].add(a+pre)
  se[0].add(a)
print(len(se[2]))

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):
            fi = False
            fj = False
            for ss in s:
                ss = int(ss)
                if not fi and ss == i:
                    fi = True
                    continue
                if fi and not fj and ss == j:
                    fj = True
                    continue
                if fj and ss == k:
                    ans += 1
                    break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
s = set()
for i in range(1000):
  p = str(i).zfill(3)
  if S.find(p[0])==-1:
    continue
  S1 = S[S.find(p[0])+1:]
  if S1.find(p[1])==-1:
    continue
  S2 = S1[S1.find(p[1])+1:]
  if S2.find(p[2])==-1:
    continue
  s.add(p)
print(len(s))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
k = 1
ans_list = []
for i in range(1000,2000):
    ansyo = str(i)
    for j in range(0,N):
        if ansyo[k] == S[j]:
            k += 1
        if k == 4:
            ans_list.append(ansyo)
            k = 1
        if j == N-1:
            k = 1
ans_list = set(ans_list)
print(len(ans_list))

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(1000):
  p = str(i).zfill(3)
  if S.find(p[0])==-1:
    continue
  S1 = S[S.find(p[0])+1:]
  if S1.find(p[1])==-1:
    continue
  S2 = S1[S1.find(p[1])+1:]
  if S2.find(p[2])==-1:
    continue
  ans+=1
print(ans)

Submission status: Accepted

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

lst = [0]*1000
sm = 0
first = [False]*10
second = [False]*100
for i in s:
    i = int(i)
    for j in range(100):
        if not second[j]:
            continue
        tmp = 10*j + i
        if lst[tmp] == 0:
            sm +=1
            lst[tmp]+=1

    for j in range(10):
        if first[j]:
            tmp = 10*j + i
            if not second[tmp]:
                second[tmp] = True

    if not first[i]:
        first[i] = True

print(sm)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N=intput()
S=input()
L1=set()
L2=set()
L3=set()
for i in range(N-1,-1,-1):
    x=int(S[i])
    for y in L2:
        L3.add(100*x+y)
    for y in L1:
        L2.add(10*x+y)
    L1.add(x)
print(len(L3))

Submission status: Accepted

Solution code:
N = input()
S = list(input())
count = 0
num_count = [False]*10
for i in range(len(S)-2):
    s_i = int(S[i])
    if num_count[s_i]:
        continue
    num_count[s_i] = True
    num_count_2 = [False]*10
    for j in range(i+1, len(S)-1):
        s_j = int(S[j])
        if num_count_2[s_j]:
            continue
        num_count_2[s_j] = True
        num_count_3 = [False] * 10
        for k in range(j+1, len(S)):
            s_k = int(S[k])
            if num_count_3[s_k]:
                continue
            num_count_3[s_k] = True
            count += 1
print(count)

Submission status: Accepted

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

for i in range(1000):
        i = '0' * (3 - len(str(i))) + str(i)
        keep = 0

        for j in range(n):
                if s[j] == i[keep]:
                        keep += 1
                if keep == 3:
                        ans += 1
                        break

print(ans)

Submission status: Accepted

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

for a in range(10):
    for b in range(10):
        for c in range(10):
            targets = (a, b, c)
            target_idx = 0
            for i in range(len(S)):
                if S[i] == targets[target_idx]:
                    target_idx += 1
                    if target_idx == len(targets):
                        ans += 1
                        break

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

n = int(readline())
s = readline().rstrip()

from collections import deque
q = deque([])

ss = set(s)

for i in ss:
    q.append(["",0,str(i)])

ans = 0
while q:
    num, ind, target = q.popleft()
    #print(num, ind, target)
    while ind < len(s):
        if s[ind] == target: #s[ind]が０から9の中のどれかを見てる
            break
        ind += 1
    else:
        continue
    num += target
    if len(num) == 3:
        ans += 1
        continue
    sss = set(s[ind+1:])
    for i in sss:
        q.append([num, ind +1, str(i)]) #出現した数字に関するキューを追加（例：一回目は024）

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

n = int(readline())
s = readline().rstrip()

from collections import deque
q = deque([])

ss = set(s)

for i in ss:
    q.append(["",0,str(i)])

ans = 0
while q:
    num, ind, target = q.popleft()
    #print(num, ind, target)
    while ind < len(s):
        if s[ind] == target: #s[ind]が０から9の中のどれかを見てる
            break
        ind += 1
    else:
        continue
    num += target
    if len(num) == 3:
        ans += 1
        continue
    for i in range(10):
        q.append([num, ind +1, str(i)]) #出現した数字に関するキューを追加（例：一回目は024）

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

n = int(readline())
s = readline().rstrip()

from collections import deque
q = deque([])

for i in range(10):
    q.append(["",0,str(i)])

ans = 0
while q:
    num, ind, target = q.popleft()
    #print(num, ind, target)
    while ind < len(s):
        if s[ind] == target: #s[ind]が０から9の中のどれかを見てる
            break
        ind += 1
    else:
        continue
    num += target
    if len(num) == 3:
        ans += 1
        continue
    for i in range(10):
        q.append([num, ind +1, str(i)]) #出現した数字に関するキューを追加（例：一回目は024）

print(ans)

Submission status: Accepted

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

ans = 0

for i in range(0, 10):
    a = S.find(str(i))
    if a == -1:
        continue
    for j in range(0, 10):
        b = S.find(str(j), a + 1)
        if b == -1:
            continue
        for k in range(0, 10):
            c = S.find(str(k), b + 1)
            if c == -1:
                continue
            ans += 1
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(0, 1000):
    if 0 <= i and i <= 9:
        num = "00"+str(i)
    elif 10 <= i and i <= 99:
        num = "0"+str(i)
    else:
        num = str(i)

    num_pos = 0
    s_pos = 0
    while True:
        if S[s_pos] == num[num_pos]:
            num_pos += 1
            if num_pos == 3:
                ans += 1
                break
        s_pos += 1
        if s_pos == N:
            break
print(ans)

Submission status: Accepted

Solution code:
# D - Lucky PIN
N = int(input())
S = input()

ans = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            a = S.find(str(i))
            if a == -1:
                continue
            # i が含まれるとき
            b = S.find(str(j),a+1)
            if not(a < b):
                continue
            # j の index が a以降のとき
            c = S.find(str(k),b+1)
            if not(b < c):
                continue
            # k の index が b　以降のとき
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
int(input())
s = list(input())
ans = 0
for i in range(10):
    if str(i) in s:
        ss = s[s.index(str(i))+1:]
        for j in range(10):
            if str(j) in ss:
                sss = ss[ss.index(str(j))+1:]
                for k in range(10):
                    if str(k) in sss:
                        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
kouho=[]
for n in range(1000):
    st=str(n)
    if len(st)<=2:
        st='0'*(3-len(st))+st
    kouho.append(st)
ans=0
for k in kouho:
    number=0
    for s in S:
        if k[number]==s:
            number+=1
        if number==3:
            break
    if number == 3:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
c=0
for i in range(1000):
  t=format(i,"0>3")
  j=0
  for k in s:
    if k==t[j]:
      j+=1
      if j==3:
        c+=1
        break
print(c)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(list)
for i in range(N):
  d[int(S[i])].append(i)
    
ans = 0
for n1 in range(10):
  if not d[n1]:continue
  for n2 in range(10):
    if not d[n2]:continue
    if d[n1][0]>d[n2][-1]: continue
    for n3 in range(10):
      if not d[n3]:continue
      if d[n1][0]>d[n3][-1] or d[n2][0]>d[n3][-1]:continue
      for idx in d[n2]:
        if idx>d[n1][0] and idx<d[n3][-1]:
          ans += 1
          break
            
print(ans)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    s = list(input())
    cnt = 0
    def f(x):
        tgt = [x[0],x[1],x[2]]
        now = 0
        global n
        for i in range(n):
            if s[i] == tgt[now]:now += 1
            if now == 3:return True
        return False
    for i in range(1000):
        if len(str(i)) == 1:
            t = "00"+str(i)
        elif len(str(i)) == 2:
            t = "0"+str(i)
        else:t = str(i)
        if f(t):cnt += 1
    print(cnt)

Submission status: Accepted

Solution code:
'''
Created on 2020/09/04

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write

  N=int(pin())
  S=pin()[:-1]

  cnt=0
  l=["0","1","2","3","4","5","6","7","8","9"]
  for i in l:
    for j in l:
      for k in l:
        c=[i,j,k]
        n=0
        for m in S:
          if m==c[n]:
            if n==2:
              cnt+=1
              break
            else:
              n+=1
  print(cnt)
  return
main()
#解説AC

Submission status: Accepted

Solution code:
N = int(input())
s = input()
cnt=0
for i in range(10):
    for j in range(10):
        for k in range(10):
            a=s.find(str(i))
            if a==-1:
                continue
            b=s[a+1:].find(str(j))
            if b==-1:
                continue
            b+=a+1
            c=s[b+1:].find(str(k))
            if c==-1:
                continue
            cnt+=1

print(cnt)

Submission status: Accepted

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

for i in range(10):
    for j in range(10):
        for k in range(10):
            a = s.find(str(k))
            if a != -1 :
                b = s.find(str(j), a + 1)
                if b != -1 :
                    c = s.find(str(i), b + 1)
                    if c != -1 :
                        cnt += 1
                    
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(1000):
    i = str(i).zfill(3)
    f = True
    x = -1
    for j in range(3):
        x = s.find(i[j],x+1)
        f = f and bool(x >= 0)
    ans += f
print(ans)

Submission status: Accepted

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

DP = [[0] * 10 for _ in range(10)]
D = [0] * 10
L = [0] * 1000
for i in range(N):
  for j in range(10):
    for k in range(10):
      if DP[j][k] == 1:
        L[j * 100 + k * 10 + int(S[i])] = 1
  for j in range(10):
    if D[j] == 1:
      DP[j][int(S[i])] = 1
  D[int(S[i])] = 1

print(sum(L))

Submission status: Accepted

Solution code:
def mitsui2019d_lucky_pin():
    import itertools
    n = int(input())
    s = input()
    cnt = 0
    for t in itertools.product(range(0, 10), repeat=2):
        si = ''.join(map(str,t))
        for i in range(n - 2):
            if si[0] != s[i]: continue
            for j in range(i + 1, n - 1):
                if si[1] != s[j]: continue
                ss = set(list(s[j+1:]))
                cnt += len(ss)
                break
            break
    print(cnt)


mitsui2019d_lucky_pin()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

# 現在のインデックスを持ちながらBFSしてみよう

N = int(readline())
S = readline().rstrip()

from collections import deque
q = deque([])

for i in range(10):
  q.append(["",0,str(i)])
  
ans = 0
while q:
  # 現在の文字列、探し始めるインデックス、探している文字列
  num, ind, target = q.popleft()
  while ind < len(S):
    if S[ind] == target:
      break
    ind += 1
  else:
    continue
  num += target
  if len(num) == 3:
    ans += 1
    continue
  for i in range(10):
    q.append([num, ind + 1, str(i)])

print(ans)