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:
N=int(input())
S=input()
s=[int(c) for c in(S)]
a=0
for i in range(10):
  flag1=-1
  f1=0
  for p in range(N):
    if flag1==-1 and s[p]==i:
      flag1=p
      f1=1
      break
  for j in range(10):
    flag2=0
    f2=0
    for q in range(flag1+1,N):
      if f1==0:
        break
      if flag2==0 and s[q]==j:
        flag2=q
        f2=1
        break
    for h in range(10):
      for r in range(flag2+1,N):
        if f2==0:
          break
        if s[r]==h:
          if f1*f2!=0:
            a+=1
          break
print(a)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline  # 改行コード「\n」を呼んでしまうため、文字数など、注意！！

n = int(input().replace("\n",""))
s = input().replace("\n","")

ans = 0
for i in range(1000):
    i = str(i).zfill(3)
    x = s.find(i[0])
    y = s.find(i[1], x+1)
    z = s.find(i[2], y+1)
    if x != -1 and y != -1 and z != -1:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math
def split(word):
    return [int(char) for char in word]

input1 = int(input())
input2 = split(str(input()))
def indexEverything(input2):
    whereDict = dict()
    for index in range(len(input2)):
        try:
            whereDict[input2[index]].append(index)
        except:
            whereDict[input2[index]] = [index]
    return whereDict

def doesCodeExist(n, whereDict):
    ##n = int(n)
    ##print(n)
    ## n is a list of the three digits
    try:
        firstLoc = whereDict[int(n[0])][0]
    except:
        ##print("what")
        return False
    ##print(firstLoc)
    found2 = False
    counter2 = 0
    ##print("getting in here")
    while not found2:
        ##print("trying")
        try:
            ##print(whereDict[int(n[1])][counter2], firstLoc)
            ##print(whereDict[int(n[1])][counter2] > firstLoc)
            if (whereDict[int(n[1])][counter2] > firstLoc):
                secondLoc = whereDict[int(n[1])][counter2]
                ##print("beep")
                ##print(secondLoc)
                found2 = True
            ##print("jebtime")
            counter2 += 1

            #print(secondLoc)
        except:
            return False
    ##print(secondLoc)
    found3 = False
    counter3 = 0
    while not found3:
        try:
            if (whereDict[int(n[2])][counter3] > secondLoc):
                ##print(whereDict[int(n[2])][counter3])
                return True
            counter3 += 1
            ##print(counter3)
        except:
            return False

whereDict = indexEverything(input2)
counter = 0
##print("here")
##print(doesCodeExist("242", whereDict))
##print("fin")
for index in range(0, 1000):
    if (index < 100):
        if (index < 10):
            index = "00" + str(index)
        else:
            index = "0" + str(index)
    if (doesCodeExist(split(str(index)), whereDict)):
        ##print(index)
        counter += 1
print(counter)

Submission status: Accepted

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

    ans = 0
    for i in range(1000):
        num = str(i).zfill(3)
        idx = 0
        for digit in S:
            if digit == num[idx]:
                idx += 1
            if idx == 3:
                ans += 1
                break
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    
    cnt = 0
    for i in range(10):
        idx_i = S.find(str(i))
        if(idx_i == -1):
            continue
        for j in range(10):
            idx_j = S.find(str(j), idx_i+1)
            if(idx_j == -1):
                continue
            for k in range(10):
                idx_k = S.find(str(k), idx_j+1)
                if(idx_k == -1):
                    continue
                cnt += 1
    print(cnt)
main()

Submission status: Accepted

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

ans = 0
for i in range(1000):
    num = str(i).zfill(3)
    flag2 = True
    start = 0
    for j in range(3):
        flag = False
        for k in range(start, N):
            if(num[j] == S[k]):
                flag = True
                start = k+1
                break
        if flag == False:
            flag2 = False
            break
    if flag2:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(1000):
    num = str(i).zfill(3)
    flag2 = True
    start = 0
    for j in range(3):
        flag = False
        for k in range(start,N):
            if num[j] == S[k]:
                flag = True
                start = k+1
                break
        if flag == False:
            flag2 = False
            break    
    if flag2:
        ans+=1
    
print(ans)

Submission status: Accepted

Solution code:
import itertools
import collections

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

password_l = [str(i).zfill(3) for i in range(1000)]

c = 0
for password in password_l:
    idx = 0
    ok = 0
    while idx != n:
        if s[idx] == password[ok]:
            ok += 1
        
        if ok == 3:
            c += 1
            break
        
        idx += 1

print(c)

Submission status: Accepted

Solution code:
nums = []
for i in range(10):
    for j in range(10):
        for k in range(10):
            nums.append(str(i) + str(j) + str(k))
n = input()
S = input()

ans = 0
for num in nums:
    index = 0
    for s in S:
        if s==num[index]:
            index+=1
        if index==3:
            ans+=1
            break
print(ans)

Submission status: Accepted

Solution code:
#python3

def main():
    n = int(input())
    s = input()
    cnt = 0
    for i in range(10):
        ind_i = s.find(str(i))    
        if ind_i == -1:
            continue
        for j in range(10):
            ind_j = s.find(str(j), ind_i+1)
            if ind_j == -1:
                continue
            for k in range(10):
                ind_k = s.find(str(k), ind_j+1)   
                if ind_k == -1:
                    continue
                cnt += 1
    print(cnt) 
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = int(input())
s = input()
ans = 0
for i in range(10):
    if str(i) in s:
        t = s[s.index(str(i))+1:]
        for j in range(10):
            if str(j) in t:
                u = t[t.index(str(j))+1:]
                for k in range(10):
                    if str(k) in u:
                        ans +=1
print(ans)

Submission status: Accepted

Solution code:
def f(s,l=3):
  if l==1:
    return len(set(s))
  a=0
  for i in set(s):
    a+=f(s[s.index(i)+1:],l-1)
  return a

n=int(input())
s=input()
print(f(s))

Submission status: Accepted

Solution code:
int(input())
n=input()
answer=0
for i in range(10):
    if str(i) in n:
        T=n[n.index(str(i))+1:]
        for j in range(10):
            if str(j) in T:
                U=T[T.index(str(j))+1:]
                for k in range(10):
                    if str(k) in U:
                        answer+=1
print(answer)

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 = cnt+1
print(cnt)

Submission status: Accepted

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

for i in range(1000):
	t = str(i).zfill(3)
	t_idx = 0
	for s_idx in range(n):
		if s[s_idx] == t[t_idx]:
			t_idx += 1
			if t_idx == 3:
				break
	if t_idx == 3:
		cnt += 1

print(cnt)

Submission status: Accepted

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

d = [[] for _ in range(10)]
for i in range(n):
    d[int(s[i])].append(i)

def lower_bound(al,k):
    if len(al)==0:
        return -1
    ng = -1
    ok = len(al)
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if al[mid]>k:
            ok = mid
        else:
            ng = mid
    if ok==len(al):
        return -1
    return ok

def check(pl):
    pos = -1
    res = []
    for i in range(3):
        pos = lower_bound(d[pl[i]], pos)
        if pos<0:
            return False
        pos = d[pl[i]][pos]
        res.append(pos)
    return True

res = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            if check([i,j,k]):
                res += 1

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
s = input()
ans = 0
for l in range(10):
    for i in range(n):
        if int(s[i]) == l:
            break
    else:
        continue
    for r in range(10):
        for j in range(i+1,n):
            if int(s[j]) == r:
                break
        else:
            continue
        num = [True]*10
        for k in range(j+1,n):
            if num[int(s[k])]:
                ans += 1
                num[int(s[k])] = False
print(ans)

Submission status: Accepted

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

def judge(A,B,C):
    s = S
    a = s.find(A)
    if a < 0:
        return False

    s = s[a+1:]
    b = s.find(B)
    if b < 0:
        return False

    s = s[b+1:]
    c = s.find(C)
    if c < 0:
        return False
    return True

l = '0123456789'
print(sum([int(bool(judge(a,b,c))) for a in l for b in l for c in l]))

Submission status: Accepted

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

def main():
    N = int(readline())
    S = readline().strip()

    ans = 0
    for p in range(1000):
        pattern = str(p).zfill(3)
        cnt = 0
        T = S
        for n in pattern:
            i = T.find(n)
            if i != -1:
                T = T[i+1:]
                cnt += 1
        if cnt == 3:
            ans += 1
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math,time
N = int(input())
S = input()

ans = 0
for i in range(1000):
    locknum = str(i).zfill(3)
    a = S.find(locknum[0])
    b = S.find(locknum[1],a+1)
    c = S.find(locknum[2],b+1)
    if a!=-1 and b!=-1 and c!=-1:
        # print(i)
        ans +=1


             
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans=[set(),set(),set()]
for i in s:
  for l in ans[1]:
      ans[2].add(l+i)
  for j in ans[0]:
        ans[1].add(j+i)
  ans[0].add(i)
print(len(ans[2]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
ans = 0
for i in range(1000):
  chk = str(i).zfill(3)
  a = s.find(chk[0])
  b = s.find(chk[1],a+1)
  c = s.find(chk[2],b+1)
  if a!=-1 and b!=-1 and c!=-1:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
def multi_index(l,x):
  return tuple([i for i,_x in enumerate(l) if _x == x])

ans = 0
for i in range(10):
  e = 0
  if str(i) in s:
    c = multi_index(s,str(i))[0]
    for j in range(10):
      if str(j) in s and i!=j and c<=len(s)-2:
        for q in range(c+1,len(s)-1):
          if str(j) == s[q]:
            d = q
            break
        else:
          d = -1
      elif str(j) in s and i==j and len(multi_index(s,str(i)))>=2:
        d = multi_index(s,str(i))[1]
      else:
        d = -1
      if d > c:
        for l in range(10):
          if str(l) in s and i!=j and j!=l and i!=l:
            e = multi_index(s,str(l))[-1]
            if e > d:
              ans += 1
          if str(l) in s and i!=j and j==l:
            e = multi_index(s,str(l))[-1]
            if e > d:
              ans += 1
          if str(l) in s and i==j and j!=l:
            e = multi_index(s,str(l))[-1]
            if e > d:
              ans += 1
          if str(l) in s and i==j and j==l:
            e = multi_index(s,str(l))[-1]
            if e > d:
              ans += 1
          if str(l) in s and i==l and j!=l:
            e = multi_index(s,str(l))[-1]
            if e > d:
              ans += 1
print(ans)

Submission status: Accepted

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

alr_1 = []
ans = []
for i in range(N-2):
    if not S[i] in alr_1:
        start_1 = S[i]
        alr_1.append(start_1)
        alr_2 = []
        for j in range(i+1, N-1):
            if not S[j] in alr_2:
                start_2 = S[j]
                alr_2.append(start_2)
                alr_3 = []
                for k in range(j+1, N):
                    if not S[k] in alr_3:
                        goal = S[k]
                        ans.append([start_1, start_2, goal])
                        alr_3.append(goal)
print(len(ans))

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
import random
import itertools

input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
_START_TIME = time.time()


def main():
    ans = solve()

    if ans is True or ans is False:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():

    _ = iip()
    S = input()

    ans = 0
    for i in range(10):
        for j in range(10):
            for k in range(10):
                n = 0

                for s in S:
                    if n == 0 and str(i) == s:
                        n =1
                    elif n == 1 and str(j) == s:
                        n = 2
                    elif n == 2 and str(k) == s:
                        ans += 1
                        break

    print(ans)



#####################################################ライブラリ集ここから




def kiriage_warizan(a, b):
    return -(-a//b)


def iip(listed=True):  # 数字のinputをlistで受け取る
    d = input().rstrip("\n").split()
    try:
        ret = [int(i) for i in d]
    except:
        ret = [int(i) if i in _NUMINT_ALL else i for i in d]
        if len(ret) == 1 and listed is False:
            return ret[0]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iipt(l, listed=True, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed))
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m

def time_debug_10percent():
    a = random.randint(0, 9)
    if a == 9:
        sys.exit()

def time_debug(t):
    time.sleep(_START_TIME + t - time.time())
    sys.exit()


def make_graph_edge_flat(N):  # グラフ作成のための辺をリストで返す
    ret = []
    for i in range(N-1):
        a, b, c = iip()
        a -= 1
        b -= 1
        ret[a].append((b, c))
        ret[b].append((a, c))
    return ret


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


def nibutan_func(func, target, left, right, side="left"): # 関数を二分探索
    l = left
    r = right
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if func(x) == target:
            return x
        elif func(x) > target:
            r = x
        else:
            l = x

    if side == "left" or func(x) == target:
        return l
    else:
        return r


def nibutan_list(list_, target, side="left"):  # リストを二分探索
    if not isinstance(list_, list):
        list_ = list(list_)

    l = 0
    r = len(list_)
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if list_[x] == target:
            return x
        elif list_[x] > target:
            r = x
        else:
            l = x

    if side == "left" or list_[x] == target:
        return l
    else:
        return r


class UfTree():
    def __init__(self, maxnum):
        self.parent = list(range(maxnum))
        self._size = [1] * maxnum
        self.rank = [0] * maxnum

    def size(self, a):
        return self._size[self.root(a)]

    def root(self, a):
        rank = 0
        cur = a
        while True:
            if self.parent[cur] == cur:
                #for i in path: # 経路圧縮
                #    self.parent[i] = cur
                return cur
            else:
                self.parent[cur] = self.parent[self.parent[cur]]
                cur = self.parent[cur]
                rank += 1

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return self

        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        return self

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
import bisect
N=int(input())
S=input()
l=[[] for i in range(10)]
for i in range(N):
    l[int(S[i])].append(i)
cnt=0
for x in itertools.product(list(range(10)),repeat=3):
    if(not l[x[0]] or not l[x[1]] or not l[x[2]]):
        continue
    zero=l[x[0]][0]
    t=bisect.bisect_left(l[x[1]],zero+1)
    if(t>=len(l[x[1]])):
        continue
    one=l[x[1]][t]
    m=bisect.bisect_left(l[x[2]],one+1)
    if(m>=len(l[x[2]])):
        continue
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
import itertools
N=int(input())
S=input()
cnt=0
for x in itertools.product(list(range(10)),repeat=3):
    li=list(x)
    zero=S.find(str(li[0]))
    one=S.find(str(li[1]),zero+1)
    two=S.find(str(li[2]),one+1)
    if(not(zero==-1 or one==-1 or two==-1)):
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = [s for s in input()]
lis = [str(i)+str(j)+str(k) for i in range(10) for j in range(10) for k in range(10)]
ans = 0
from collections import defaultdict
d = defaultdict(list)
for i,s in enumerate(S):
    d[s].append(i)

for s in lis:
    if d[s[0]]:
        i = min(d[s[0]])
        if d[s[1]]:
            for j in d[s[1]]:
                if j > i:
                    if d[s[2]]:
                        if max(d[s[2]]) > j:
                            ans += 1
                            break
                    break
        else:continue
    else:continue
                            
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = 0
for i in range(1000):
    k = str(i).zfill(3)
    c = 0
    for j in s:
        if k[c]==j:
            c+=1
            if c==3:
                ans+=1
                break
print(ans)

Submission status: Accepted

Solution code:
import math,time
N = int(input())
S = input()
# length = len(S)
ans = 0
t1 = time.time()
for i in range(1000):
    locknum = str(i).zfill(3)
    a = S.find(locknum[0])
    b = S.find(locknum[1],a+1)
    c = S.find(locknum[2],b+1)
    if a!=-1 and b!=-1 and c!=-1:
        # print(i)
        ans +=1

t2 = time.time()
# print(t2-t1)                 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(1000):
    x = str(i)
    if len(x) == 1: x = '0'*2+x
    elif len(x) == 2:x = '0'+x
    res = 0
    mae = -1
    for e in x:
        for i in range(n):
            if s[i] == e and i > mae:
                res+=1
                mae = i
                break
    if res == 3:
        ans+=1
        
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
Nd=[[] for i in range(10)]
Ns=[0 for i in range(10)]
Nl=[-1 for i in range(10)]
Nr=[-1 for i in range(10)]
for i,s in enumerate(S):
  if Ns[int(s)]==0:
    Nl[int(s)]=i
  
  Ns[int(s)]+=1
  Nr[int(s)]=i
  for j in range(10):
    Nd[j].append(Ns[j])
#print(Ns)
#print(Nd)

    
ans=0    
for i in range(10):
  if Nl[i]==-1:
    continue
  for j in range(10):
    if Nl[j]==-1:
      continue
    for k in range(10):
      if Nl[k]==-1:
        continue
      a=Nl[i]
      c=Nr[k]
      if Nd[j][max(c-1,0)]-Nd[j][a]>0:
        ans+=1
print(ans)

Submission status: Accepted

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

td = [[[0 for i in range(10)] for j in range(10)]  for j in range(10)]
sd = [[0 for i in range(10)] for i in range(10)]
fd = [0 for i in range(10)]

for i in range(N):
  s = int(S[i])
  #三桁目の更新
  for q in range(10):
    for r in range(10):
      if sd[q][r] == 1:
        td[q][r][s] = 1
  #二桁目の更新
  for p in range(10):
    if fd[p] == 1:
      sd[p][s] = 1           
  #一桁目の更新
  if fd[s] == 0:
    fd[s] = 1

 
###
c = 0
for i in range(10):
  for j in range(10):
    c += td[i][j].count(1)  
print(c)

Submission status: Accepted

Solution code:
import collections

N = int(input())
S = [int(i) for i in list(input())]

count = 0
for i in range(10):
    for num,j in enumerate(S):
        if j == i:
            T = S[num+1::]
            for k in list(set(T)):
                for num2, l in enumerate(T):
                    if k == l:
                        U = T[num2+1::]
                        count += len(set(U))
                        break
            
            break
            
print(count)

Submission status: Accepted

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

result_dic = {}

before_words = {i:0 for i in range(10)}
after_words = {i:0 for i in range(10)}
for s in S:
    after_words[int(s)] += 1

for  i, s in enumerate(S[:-1]):
    if i == 0:
        before_words[int(s)] += 1
        after_words[int(s)] -= 1
        continue
    after_words[int(s)] -= 1

    before =  [x for x, y in before_words.items() if y > 0]
    after =  [x for x, y in after_words.items() if y > 0]
    for b in before:
        for a in after:
            result_dic[str(b) + s + str(a)] = 1

    before_words[int(s)] += 1

print(len(result_dic))

Submission status: Accepted

Solution code:
# 三井住友信託銀行プログラミングコンテスト2019
# D - Lucky PIN
# https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_d

N = int(input())
*S, = map(int, input())

# 最大でも000-999までの1000通り
# dp[i][j]: i(index)より左にあるj(0-9)で最も右にあるもののindex

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

cnt = 0
for i in range(1000):
    a = i//100 % 10
    b = i//10 % 10
    c = i % 10

    ci = dp[N][c]
    if ci != -1:
        bi = dp[ci][b]
        if bi != -1:
            ai = dp[bi][a]
            if ai != -1:
                cnt += 1

print(cnt)

Submission status: Accepted

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

cnt = 0
for i in range(1000):
    
    t = str(i)
    u = '0'*(3-len(t))+t
    
    j = 0
    p = u[0]
    for x in s:
        if x == p:
            j += 1
            if j == 3:
                cnt += 1
                break
            p = u[j]
            
print(cnt)

Submission status: Accepted

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

ans=0
for i in range(1000):
  t=str(i)
  u="0"*(3-len(t))+t
  j=0
  p=u[0]
  for x in s:
    if x==p:
      j+=1
      if j==3:
        ans+=1
        break
      p=u[j]
    
print(ans)

Submission status: Accepted

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

ans=0
for i in range(1000):
  t=str(i)
  u="0"*(3-len(t))+t
  j=0
  exi=0
  p=u[0]
  for x in s:
    if x==p:
      j+=1
      if j==3:
        ans+=1
        break
      p=u[j]
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
list=["" for _ in range(1000)]
ans=0
for i in range(1000):
  t=str(i)
  u="0"*(3-len(t))+t
  j=0
  exi=0
  p=u[0]
  for x in s:
    if x==p:
      j+=1
      if j==3:
        ans+=1
        break
      p=u[j]
    
print(ans)

Submission status: Accepted

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

candidates = set([""])
keys = set()

for s in S:
    new_candidate = set()
    for cand in candidates:
        new = cand + s
        if len(new) == 3:
            keys.add(new)
        else:
            new_candidate.add(new)
    candidates = candidates.union(new_candidate)

print(len(keys))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

n = int(input())
s = list(input())
s = [int(i) for i in s]


def is_valid(s, pin):
    i, j, k = pin
    s_tmp = s[:]
    if i not in s_tmp:
        return False
    i_idx = s_tmp.index(i)
    s_tmp = s_tmp[i_idx + 1:]
    if j not in s_tmp:
        return False
    j_idx = s_tmp.index(j)
    s_tmp = s_tmp[j_idx + 1:]
    if k not in s_tmp:
        return False
    return True


num = 10
res = 0
for i in range(num):
    for j in range(num):
        for k in range(num):
            pin = [i, j, k]
            if is_valid(s, pin):
                # print(f"{pin} is valid pin")
                res += 1

print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = set()
for first in range(10):
    for second in range(10):
        f_flag = False
        s_flag = False
        for i in S:
            if f_flag is False:
                if int(i) == first:
                    f_flag = True
            elif s_flag is False:
                if int(i) == second:
                    s_flag = True
            else:
                ans.add(str(first)+str(second)+i)

print(len(ans))

Submission status: Accepted

Solution code:
import pprint
import copy
N=int(input())
S=input()
dig=[[] for i in range(10)]
chkigid=[0]*10
ansdigid=[0]*10
#print(dig)
cnt=0
for i in range(N):
    dig[int(S[i])].append(i)#dig[int(S[i])][chkigid[int(S[i])]][2]=i
    chkigid[int(S[i])]=chkigid[int(S[i])]+1
    #pprint.pprint(dig)
    #pprint.pprint(chkigid)
    #input()
#print(dig)
for i in range(10):
    for j in range(10):
        #dig2=copy.deepcopy(dig)
        v1 = float("inf")
        if len(dig[i])!=0:
            v1 = dig[i][0]
        v2 = float("inf")
        if len(dig[j])!=0:
            v2= dig[j][len(dig[j])-1]
        if (v1<v2)and(v1!=float("inf"))and(v2!=float("inf")):
            for k in range(10):
                for l in range(len(dig[k])):
                    if v1<dig[k][l]<v2:
                        #print(dig2[k][l][0])
                        cnt=cnt+1
                        break
        #pprint.pprint(dig)
        #print(i,v1,j,v2,cnt)
        #input()
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
keys = [str(i) for i in range(1000)]
count = 0
for i in range(10):
    keys[i] = '00' + keys[i]
for i in range(10,100):
    keys[i] = '0' + keys[i]
for i in keys:
    temp1 = i
    temp2 = ''
    if temp1[0] in S:
        search = S.find(temp1[0])+1
        if search >= len(S):
            continue
        temp2 = S[search:]
        if temp1[1] in temp2:
            search = temp2.find(temp1[1])+1
            if search >= len(temp2):
                continue
            temp2 = temp2[search:]
            if temp1[2] in temp2:
                count += 1
print(count)