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

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main{
    static MyReader in = new MyReader();
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args){
        int N = in.i();
        char[] S = in.s().toCharArray();

        HashSet<Integer>[] one = new HashSet[N];
        one[N-1] = new HashSet<>();
        one[N-1].add(S[N-1]-'0');
        for(int i = N-2; i > -1; i--){
            one[i] = new HashSet<>(one[i+1]);
            one[i].add(S[i]-'0');
            if(one[i].size() == 10){
                for(int j = i-1; j > -1; j--){
                    one[j] = one[i];
                }
                break;
            }
        }

        HashSet<Integer>[] two = new HashSet[N];
        two[N-1] = new HashSet<>();
        for(int i = N-2; i > -1; i--){
            two[i] = new HashSet<>(two[i+1]);
            int ii = 10*(S[i]-'0');
            for(Integer j : one[i+1]){
                two[i].add(ii+j);
            }
            if(two[i].size() == 100){
                for(int j = i-1; j > -1; j--){
                    two[j] = two[i];
                }
                break;
            }
        }

        HashSet<Integer> three = new HashSet<>();
        for(int i = N-3; i > -1; i--){
            int ii = 100*(S[i]-'0');
            for(Integer j : two[i+1]){
                three.add(ii+j);
            }
            if(three.size() == 1000){
                System.out.println(1000);
                return;
            }
        }
        System.out.println(three.size());
    }

    static class MyReader extends BufferedReader{
        MyReader(){
            super(new InputStreamReader(System.in));
        }

        String s(){
            try{return readLine();}catch(IOException e){return "";}
        }

        String[] ss(){
            return s().split(" ");
        }

        int i(){
            return Integer.parseInt(s());
        }

        int[] ii(){
            String[] ss = ss();
            int[] ii = new int[ss.length];
            for(int j = 0; j < ss.length; j++) ii[j] = Integer.parseInt(ss[j]);
            return ii;
        }

        ArrayList<Integer> li(){
            String[] ss = ss();
            ArrayList<Integer> li = new ArrayList<>();
            for(int j = 0; j < ss.length; j++) li.add(Integer.parseInt(ss[j]));
            return li;
        }

        long l(){
            return Long.parseLong(s());
        }

        long[] ll(){
            String[] ss = ss();
            long[] ll = new long[ss.length];
            for(int j = 0; j < ss.length; j++) ll[j] = Long.parseLong(ss[j]);
            return ll;
        }
    }
}