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:
def main():
    N = int(input())
    S = input()
    ans = 0
    for v in range(1000):
        cur = 0
        v_s = str(v)
        if len(v_s) == 1:
            v_s = "00" + v_s
        elif len(v_s) == 2:
            v_s = "0" + v_s
        for s in S:
            if v_s[cur] == s:
                cur += 1
            if 2 < cur:
                ans += 1
                break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math
 
#import bisect
 
def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
 
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
 
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
 
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
 
    return result
 
def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta
 
def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
 
 
def main():
#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
    n=int(input())
    s=list(map(str,input()))
    a=[]
    for i in range(1000):
        if i<10:
            a.append("00"+str(i))
        elif i>=10 and i<100:
            a.append("0"+str(i))
        else:
            a.append(str(i))
#    print(s)
#    print(a)
    count=0
    for i in range(1000):
        keta1=a[i][0]
        keta2=a[i][1]
        keta3=a[i][2]
   #     print(keta1,keta2,keta3)
        x=my_index(s,keta1,n+1)
        if x>=n-1:
            continue
 
        y = my_index(s[x+1:], keta2,n+1)
        if y>=len(s[x+1:])-1:
            continue
        z = my_index(s[x+y+2:], keta3,n+1)
        if z>len(s[x+y+2:]):
            continue
        if x!=n+1 and y!=n+1 and z!=n+1:
        #    print(s[x],s[y],s[z],i)
            count+=1
    print(count)
 
#  print(c)
 
 
 
 
            #    print(n,kingaku(a,b,x))
 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
S = input()
ans = 0
for i in range(1000):
    num = str(i).zfill(3)
    _idx = 0
    flag = True
    for n in num:
        ind = S[_idx:].find(n)
        if ind==-1:
            flag = False
            break
        else:
            _idx = _idx + ind+1
    if flag:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N= int(input())
S = input()
ans = 0
#find(探す文字、開始位置、終了位置)
for i in range(10):
  x = S.find(str(i))
  if x == -1:
    continue
  for j in range(10):
    y = S.find(str(j), x+1)
    if y == -1:
      continue
    for k in range(10):
      z = S.find(str(k), y+1)
      if z != -1:
        ans += 1
      
print(ans)

Submission status: Accepted

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

for i in range(10):
    if i in S:
        ind_i = S.index(i)
        T = S[(ind_i + 1):]
        for k in range(10):
            if k in T:
                ind_k = T.index(k)
                for l in range(10):
                    if l in T[(ind_k + 1):]:
                        count += 1

print(count)

Submission status: Accepted

Solution code:
import sys; readline = sys.stdin.readline
from collections import deque
 
N = int(readline())
S = readline().rstrip()
 
e1, e2, e3 = set([]), set([]), set([])
 
for i in range(N):
    for s2 in e2:
        e3.add(s2 + S[i])
    
    for s1 in e1:
        e2.add(s1 + S[i])
 
    e1.add(S[i])
 
print(len(e3))

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math
 
#import bisect
 
def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
 
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
 
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
 
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
 
    return result
 
def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta
 
def main():
#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
    n=int(input())
    s=list(map(int,input()))
    c = [[-2 for j in range(11)] for i in range(10)]
    d={0}
 #   print(c)
 
    for i in range(n):
        for j in range(10):
            if c[j][10] > -2:
                for k in range(10):
                    if c[j][k]>-2:
                        d.add(str(j)+str(k)+str(s[i]))
                c[j][s[i]]=-1
        c[s[i]][10] = -1
#    print(d)
 
 #   print(c)
    for i in range(100):
        d.discard(str(i))
    d.discard(0)
#    print(d)
    print(len(d))
            #    print(n,kingaku(a,b,x))
 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def int2str(i):
  t=str(i)
  if i <10:
    a="0"
    b="0"
    c=t[-1]
  elif i <100:
    a="0"
    b=t[-2]
    c=t[-1]
  else:
    a=t[-3]
    b=t[-2]
    c=t[-1]
  return a,b,c

N=int(input())
S=input()
ans=0
for i in range(1000):
  S1=[]
  S2=[]
  a,b,c=int2str(i)
  flag=0
  i1 = S.find(a)
  if i1 !=-1:
    S1=S[i1+1:]
    i2=S1.find(b)
    if i2 !=-1:
      S2=S1[i2+1:]
      i3=S2.find(c)
      if i3 !=-1:
        ans+=1
print(ans)

Submission status: Accepted

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

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 = S.find(str(i))
            if fi < 0:
                continue
            fj = S[fi+1:].find(str(j))
            if fj < 0:
                continue
            fk = S[fi+fj+2:].find(str(k))
            if fk < 0:
                continue
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math

#import bisect

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

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


def main():
#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
    n=int(input())
    s=list(map(str,input()))
    a=[]
    for i in range(1000):
        if i<10:
            a.append("00"+str(i))
        elif i>=10 and i<100:
            a.append("0"+str(i))
        else:
            a.append(str(i))
#    print(s)
#    print(a)
    count=0
    for i in range(1000):
        keta1=a[i][0]
        keta2=a[i][1]
        keta3=a[i][2]
   #     print(keta1,keta2,keta3)
        x=my_index(s,keta1,n+1)
        if x>=n-1:
            continue

        y = my_index(s[x+1:], keta2,n+1)
        if y>=len(s[x+1:])-1:
            continue
        z = my_index(s[x+y+2:], keta3,n+1)
        if z>len(s[x+y+2:]):
            continue
        if x!=n+1 and y!=n+1 and z!=n+1:
        #    print(s[x],s[y],s[z],i)
            count+=1
    print(count)

#  print(c)




            #    print(n,kingaku(a,b,x))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def aruka(a,b,c):
    i = 0
    while i <= n:
        if i == n:
            return False
        if s[i] == a:
            i += 1
            break
        i += 1
    while i <= n:
        if i == n:
            return False
        if s[i] == b:
            i += 1
            break
        i += 1
    while i <= n:
        if i == n:
            return False
        if s[i] == c:
            return True
        i += 1


ans = 0

for i in range(10):
    for j in range(10):
        for k in range(10):
            if aruka(i,j,k):
                ans += 1

print(ans)

Submission status: Accepted

Solution code:
n= int(input())
s = input()
ans = 0
for i in range(10):
  ii = s.find(str(i))
  if -1 == ii:
    continue
  for j in range(10):
    ij = s.find(str(j), ii+1)
    if -1 == ij:
      continue
    for k in range(10):
      ik = s.find(str(k), ij+1)
      if ik != -1:
        ans += 1
      
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(0,10):
    for j in range(0,10):
        for k in range(0,10):
            p1 = s.find(str(i))
            if p1 == -1:
                continue
            p2 = s.find(str(j),p1+1)
            if p2 == -1:
                continue
            if s.find(str(k),p2+1) != -1:
                ans += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
S=list(map(int,list(input())))

RL=list(range(0,10))
LL=list(range(0,10))
ans=0
for x in RL:
    for i,c in enumerate(S):
        if c==x:
            x_i=i
            break
    else:continue
    for y in LL:
        for i,c in enumerate(S[::-1]):
            if c==y:
                y_i=N-i-1
                break
        else:continue
        if x_i<y_i:
            ans+=len(set(S[x_i+1:y_i]))
print(ans)

Submission status: Accepted

Solution code:
def main():
    # input = sys.stdin.readline
    n = int(input())
    pin = input()

    count = 0

    for i in range(1000):
        password = '00' + str(i)
        password = password[-3:]

        v1 = password[0]
        v2 = password[1]
        v3 = password[2]

        if v1 in pin:
            p1 = pin.index(v1)
            pin2 = pin[p1+1:]
        else:
            continue
        if v2 in pin2:
            p2 = pin2.index(v2)
            pin3 = pin2[p2+1:]
        else:
            continue
        if v3 in pin3:
            count += 1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if N < 3:
    print(0)
    exit()

ans = 0
list1=[]
for i,s in enumerate(S[:-2]):
    if s not in list1:
        list1.append(s)
        list2=[]
        for j,t in enumerate(S[i+1:-1]):
            if t not in list2:
                list2.append(t)
                list3=[]
                for k,u in enumerate(S[i+j+2:]):
                    if u not in list3:
                        ans+=1
                        list3.append(u)

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:]
        # print(t)
        for j in range(10):
            if str(j) in t:
                u = t[t.index(str(j))+1:]
                # print(u)
                for k in range(10):
                    if str(k) in u:
                        #print(i, j, k)
                        ans += 1

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

#+++++
		
def main():
	n = int(input())
	s = input()
	count=0
	for i in range(1000):
		ss=str(i)
		if i < 10:
			ss = '00'+ss
		elif i < 100:
			ss='0'+ss
			
		if ss[0] in s:
			iia = s.index(ss[0])
		else:
			continue
		#pa(ii)
		if ss[1] in s[iia+1:]:
			iib = s[iia+1:].index(ss[1])+iia+1
		else:
			continue
		#pa(ii)
		if ss[2] in s[iib + 1:]:
			iic = s[iib+1:].index(ss[2])+iib+1
		else:
			continue
			
		#pa((ss,i))
		#pa((iia,iib,iic,s[iia],s[iib],s[iic]))
		count+=1
			
	print(count)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
from bisect import bisect_left
N=int(input())
S=input()
li=[[] for i in range(10)]
for i in range(N):
    n=int(S[i])
    li[n].append(i)
ans=0
for i in range(10**3):
    a=i//100
    b=(i-100*a)//10
    c=i-100*a-10*b
    tmpn=[0 for i in range(10)]
    if not li[a]:
        continue
    tmpi=li[a][0]
    #print(tmpi)
    tmpn[a]+=1
    if tmpn[b]>len(li[b])-1:
        continue
    indin=bisect_left(li[b],tmpi,tmpn[b],len(li[b]))
    if indin==len(li[b]):
        continue
    tmpi=li[b][indin]
    #print(tmpi)
    #tmpn[b]+=1
    tmpn[b]=indin+1
    #print(tmpn)
    if tmpn[c]>len(li[c])-1:
        continue
    indin=bisect_left(li[c],tmpi,tmpn[c],len(li[c]))
    #print("indin",indin)
    if indin!=len(li[c]):
        ans+=1
        #print(i)
print(ans)

Submission status: Accepted

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

used1s = [False] * 10
used2s = [False] * 100
used3s = [False] * 1000

for S in Ss:
    S = int(S)
    for x in range(100):
        if used2s[x]:
            used3s[x*10+S] = True
    for x in range(10):
        if used1s[x]:
            used2s[x*10+S] = True
    used1s[S] = True

ans = used3s.count(True)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
f=set()
s=set()
t=set()
for x in map(int,list(input())):
	for y in s:
		t.add(10*y+x)
	for y in f:
		s.add(10*y+x)
	f.add(x)
print(len(t))

Submission status: Accepted

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

result = 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:
            result += 1

print(result)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input())
    S = input().rstrip()
    ans = 0
    for i in range(1000):
        i = str(i).zfill(3)
        prev = -1
        for s in i:
            pos = S.find(s, prev + 1)
            if pos > prev:
                prev = pos
            else:
                break
        else:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


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

count=set()
nums=set()
values2=set()
for n in range(0,N-2):
    #print(n,nums)
    if S[n] in nums:
        continue
    else:
        nums.add(S[n])
    values2.clear()
    for m in range(n+1,N-1):
        #print(n,m)
        if S[m] in values2:
            continue
        else:
            values2.add(S[m])
        for s in set(S[m+1:]):

            l=S[n]+S[m]+s
            #print(l)
            if not l in count:
                count.add(l)
print(len(count))

Submission status: Accepted

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

li = [[] for _ in range(10)]

for i in range(n):
    t = int(s[i])
    li[t].append(i)

a = 0
b = 0
c = 0
cnt = 0

for i in range(10):
  for j in range(10):
    for k in range(10):
      b = 0
      a = 0
      
      if len(li[i]) > 0:
        a = li[i][0]
          
        for d in li[j]:
            if d > a:
                b = d
                break
        else:
            break
        
        for e in li[k]:
            if e > b:
                cnt += 1
                break
        

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(0,10):
    si=s.find(str(i))
    if si<0:
        continue
    for j in range(0,10):
        sj=s[si+1:].find(str(j))
        if sj<0:
            continue
        for k in range(0,10):
            sk=s[si+1:][sj+1:].find(str(k))
            if sk>=0:
                ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict, deque
from copy import deepcopy
import itertools
# 文字列の場合は普通のinput()を使うか下を使う
# input = sys.stdin.readline().rstrip()

N = int(input())
S = input()
q = deque([])
q_new = deque([])
d = defaultdict(list)
for i in range(10):
    for j in range(10):
        for k in range(10):
            d[i].append([k, j, i])

ans = 0
for x in S:
    x = int(x)
    q = deepcopy(d[x])
    d[x] = []
    for key in q:
        l = len(key)
        if l == 3:
            d[key[1]].append(key[:2])
        elif l == 2:
            d[key[0]].append(key[:1])
        elif l == 1:
            ans += 1

print(ans)

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
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 GI(V,E,DAG=False,index=0):
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[1:])
            bb=(inp[1:])
        g[a].append(b)
        if g[b].append(a)
        h.append(b)
'''

def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()
 
show_flg=False
show_flg=True

ans=0

n=I()
s=input()
m={}
d1=set()
d2=set()
d3=set()
ans=set()
for i in s:
    
    for a,b in d2:
        ans.add((a,b,i))

    for a,b in d2:
        d3.add((a,b,i))
    for d in d1:
        d2.add((d,i))
    d1.add(i)

print(len(ans))

Submission status: Accepted

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

bangos = []

for i in range(10):
    for j in range(10):
        for k in range(10):
            moji = ""
            moji += str(i)
            moji += str(j)
            moji += str(k)
            bangos.append(moji)

ans = 0
for val in bangos:
    j = 0
    for i in N:
        if val[0] == i:
            j += 1
            break
        else:
            j += 1
    if j > n-1:
        continue
    tmp = N[j:]
    for i in str(tmp):
        if val[1] == i:
            j += 1
            break
        else:
            j += 1
    if j > n-1:
        continue
    tmp = N[j:]
    for i in str(tmp):
        if val[2] == i:
            ans += 1
            break
        else:
            j += 1

print(ans)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int,list(input())))
cnt = 0
for i in range(10):
	if i in b:
		one = b.index(i)
		if one < a-1:
			bb = b[one+1:]
			for j in range(10):
				if j in bb:
					two = bb.index(j)
					if two < a-1:
						bbb = bb[two+1:]
						for k in range(10):
							if k in bbb:
								three = bbb.index(k)
								cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
lists = ['{0:03d}'.format(i) for i in range(1000)]
import re
def check(target, a):
    ans = re.search(a, target)
    if ans == None:
        return None
    else:
        return target[ans.start()+1:]

def print_ans(N, S):
    """Test Case
    >>> print_ans(4, "0224")
    3
    >>> print_ans(6, "123123")
    17
    >>> print_ans(19, "3141592653589793238")
    329
    """
    ans = 0
    for a in lists:
        tmp = S
        for i in range(3):
            tmp = check(tmp, a[i])
            if tmp == None:
                break
        if tmp != None:
            ans += 1
    print(ans)
    
if __name__ == '__main__':
    N = int(input().rstrip())
    S = input().rstrip()
    print_ans(N, S)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
s=input()
dp=[[0]*10,[0]*100,[0]*1000]
for i in range(n):
    for j in range(100):
        if dp[1][j]>=1:
            # print(j*10+int(s[i]))
            dp[2][j*10+int(s[i])]+=1
    for j in range(10):
        if dp[0][j]>=1:
            dp[1][j*10+int(s[i])]+=1
            # print(j*10+int(s[i]))
    dp[0][int(s[i])]+=1
ans=0
# print(dp[0])
# print(dp[1])
for i in range(1000):
    if dp[2][i]>=1:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
import bisect
import collections

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


def solve(N, S):
    d = [collections.deque() for i in range(10)]
    count = 0
    for i in range(N):
        s = int(S[i])
        d[s].append(i)
    for p in range(1000):
        T = '{:03d}'.format(p)
        p0 = int(T[0])
        p1 = int(T[1])
        p2 = int(T[2])
        k0 = 0
        if len(d[p0]) > k0:
            idx0 = d[p0][k0]
            k1 = bisect.bisect_left(d[p1], idx0+1)
            if len(d[p1]) > k1:
                idx1 = d[p1][k1]
                k2 = bisect.bisect_left(d[p2], idx1+1)
                if len(d[p2]) > k2:
                    idx2 = d[p2][k2]
                    if  S[idx0] + S[idx1] + S[idx2] == T:
                        count += 1
    return count
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)

Submission status: Accepted

Solution code:
import bisect
import numpy as np
N = int(input())
S = input()

dic_ = {}
array =  np.zeros((10, 300001), np.int8)

for i in range(N):
    array[int(S[i]), i] = 1

def get_first_pos(i, k):
    ar = array[i, k+1:]
    wh = np.where(ar>0)
    if len(wh[0]) > 0:
        return wh[0][0] + k+1
    return None

ans = 0
        
for i in range(10):
    x1 = get_first_pos(i, -1)
    if x1 is None:
        continue
    for j in range(10):
        x2 = get_first_pos(j, x1)
        if x2 is None:       
            continue
        for k in range(10):
            x3 = get_first_pos(k, x2)
            if x3 is None:       
                continue
            ans += 1

print(ans)

Submission status: Accepted

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

def cal(x, a):
    for aa in a:
        if aa > x:
            return aa
    return False

li = [[] for _ in range(10)]
for i, n in enumerate(S):
    num = int(n)
    li[num].append(i)
    
cnt = 0
flag = True
for i in range(10):
    if len(li[i]) > 0:
        for j in range(10):
            if len(li[j]) > 0:
                ret = cal(li[i][0], li[j])
                if  ret == False:
                    flag = False
                else: 
                    flag = True
                for k in range(10):
                    if len(li[k]) > 0 and flag == True:
                        ret2=cal(ret, li[k]) 
                        if  ret2 != False:
                            cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = list(map(int, input()))
ans = 0
first = [0 for _ in range(10)]
lst = [[0 for _ in range(10)] for _ in range(10)]

for i in range(N):
	ima = 100
	if first[S[i]] == 0:
		first[S[i]] = 1
		ima = S[i]
	for j in range(10):
		if ima != j and first[j] == 1 and lst[j][S[i]] == 0:
			lst[j][S[i]] = 1
			ans += len(collections.Counter(S[i + 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:
a = int(input())
b = input()
def my_count(l):
  p = 0
  for i in range(10):
    i = str(i)
    if i in l:
      p = p + 1
  return p
s = []
ans = 0
for i in range(10):
  k = str(i)  
  s.append(b.find(k))
  if s[i] >= 0:
    for j in range(10):
      h = str(j)  
      y = b[s[i]+1:len(b)]
      t = y.find(h)
      if t >= 0:
        c = y[t+1:len(b)]
        ans = ans + my_count(c)
print(ans)

Submission status: Accepted

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

ary1 = [0 for i in range(10)]
ary2 = [[0 for i in range(10)] for j in range(10)]
ary3 = [[[0 for i in range(10)] for j in range(10)] for k in range(10)]

for ss in s:
  for i in range(10):
    for j in range(10):
      if ary1[i] == 1 and ary2[i][j] == 1:
        ary3[i][j][ss] = 1
  for i in range(10):
    if ary1[i] == 1:
      ary2[i][ss] = 1
  ary1[ss] = 1

numbers = set()

def exists(i, j, k, s):
  return ary3[i][j][k] == 1

for i in range(0, 10):
  for j in range(0, 10):
    for k in range(0, 10):
      if exists(i, j, k, s):
        numbers.add(str(i) + str(j) + str(k))

# print(numbers)
print(len(numbers))

Submission status: Accepted

Solution code:
N  = int(input())
S = input()
idx_arr = [[] for _ in range(10)]
for i in range(N):
  idx_arr[int(S[i])].append(i)
cnt = 0

for j in range(1000):
  target = str("{0:03d}".format(j))
  flag = True
  idx = -1
  for k in range(len(target)):
    if [l for l in idx_arr[int(target[k])] if l > idx]!=[]:
      idx = min([l for l in idx_arr[int(target[k])] if l > idx])
    else:
      flag = False
      break
  if flag:
    cnt += 1
print(cnt)

Submission status: Accepted

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

indexs=[[] for i in range(10)]
for i in range(N):
  indexs[int(S[i])].append(i)
  
counter=0
for i in range(0, 1000):
  a=i//100
  b=(i%100)//10
  c=i%10
  
  if len(indexs[a])>0 and len(indexs[b])>0 and len(indexs[c])>0:
    x=min(indexs[a])
    z=max(indexs[c])
    n=len(indexs[b])
    j=0
    for i in range(n):
      if indexs[b][i]>x and indexs[b][i]<z:
        j=1
    counter+=j
  
print(counter)

Submission status: Accepted

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

lset = [0]
for c in S:
    lset.append(lset[-1] | (1<<int(c)))

rset = [0]
for c in S[::-1]:
    rset.append(rset[-1] | (1<<int(c)))
rset.reverse()

ptns = [set() for i in range(10)]
for i,c in enumerate(S):
    for j in range(10):
        if lset[i] & (1<<j) == 0: continue
        for k in range(10):
            if rset[i+1] & (1<<k) == 0: continue
            ptns[int(c)].add(j*10 + k)

print(sum(len(p) for p in ptns))

Submission status: Accepted

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

s = set()
sub = set()
for i in range(0, N - 2):
    s1 = S[i]
    if s1 in sub:
        continue
    sub.add(s1)
    for j in range(i + 1, N - 1):
        s2 = s1 + S[j]
        if s2 in sub:
            continue
        sub.add(s2)
        for k in range(j + 1, N):
            s3 = s2+S[k]
            s.add(s3)

print(len(s))

Submission status: Accepted

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

res = [0 for i in range(1000)]

for i in range(1000):
  tgt = str(i).zfill(3)
  tmpa = s.find(tgt[0])
  if tmpa < 0:
    pass
  else:
    sa = s[(tmpa+1):]
    tmpb = sa.find(tgt[1])
    if tmpb < 0:
      pass
    else:
      sb = sa[(tmpb+1):]
      tmpc = sb.find(tgt[2])
      if tmpc < 0:
        pass
      else:
        res[int(tgt)] = 1

print(sum(res))

Submission status: Accepted

Solution code:
n=int(input())
ls=list(input())
nums={'0':[],'1':[],'2':[],'3':[],'4':[],'5':[],'6':[],'7':[],'8':[],'9':[]}
for idx,num in enumerate(ls):
    nums.setdefault(num,[]).append(idx)
count=0
for i in range(1000):
    i_zero=str(i).zfill(3)
    cand=list(i_zero)
    first_ls=nums.get(cand[0])
    if not first_ls:
        continue
    first=first_ls[0]
    last_ls=nums.get(cand[2])
    if not last_ls:
        continue
    last=last_ls[-1]
    second_ls=nums.get(cand[1])
    flag=False
    for j in second_ls:
        if j>first and j<last:
            flag=True
    if flag:
        count+=1
print(count)