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 C# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;


class Program
{
    static void Main(string[] args)
    {
        // 入力
        //var s = Console.ReadLine();
        var n = int.Parse(Console.ReadLine());
        var s = Console.ReadLine();
        //int[] n = Console.ReadLine().Split().Select(int.Parse).ToArray();

        var set = new HashSet<string>();

        bool[] zero = new bool[s.Length];
        bool[] one = new bool[s.Length];
        bool[] two = new bool[s.Length];
        bool[] three = new bool[s.Length];
        bool[] four = new bool[s.Length];
        bool[] five = new bool[s.Length];
        bool[] six = new bool[s.Length];
        bool[] seven = new bool[s.Length];
        bool[] eight = new bool[s.Length];
        bool[] nine = new bool[s.Length];
        for (int i = s.Length - 1; 0 < i; i--)
        {
            if (s[i] == '0') zero[i] = true;
            if (s[i] == '1') one[i] = true;
            if (s[i] == '2') two[i] = true;
            if (s[i] == '3') three[i] = true;
            if (s[i] == '4') four[i] = true;
            if (s[i] == '5') five[i] = true;
            if (s[i] == '6') six[i] = true;
            if (s[i] == '7') seven[i] = true;
            if (s[i] == '8') eight[i] = true;
            if (s[i] == '9') nine[i] = true;

            if (i + 1 < s.Length && zero[i + 1] == true) zero[i] = true;
            if (i + 1 < s.Length && one[i + 1] == true) one[i] = true;
            if (i + 1 < s.Length && two[i + 1] == true) two[i] = true;
            if (i + 1 < s.Length && three[i + 1] == true) three[i] = true;
            if (i + 1 < s.Length && four[i + 1] == true) four[i] = true;
            if (i + 1 < s.Length && five[i + 1] == true) five[i] = true;
            if (i + 1 < s.Length && six[i + 1] == true) six[i] = true;
            if (i + 1 < s.Length && seven[i + 1] == true) seven[i] = true;
            if (i + 1 < s.Length && eight[i + 1] == true) eight[i] = true;
            if (i + 1 < s.Length && nine[i + 1] == true) nine[i] = true;
        }


        var ans = 0;
        for (int i = 0; i < n; i++)
        {
            var ok0 = false;
            var ok1 = false;
            var ok2 = false;
            var ok3 = false;
            var ok4 = false;
            var ok5 = false;
            var ok6 = false;
            var ok7 = false;
            var ok8 = false;
            var ok9 = false;

            for (int k = i + 1; k < n - 1; k++)
            {
                if (ok0 && ok1 && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8 && ok9) break;
                if (ok0 && s[k] == '0') continue;
                if (ok1 && s[k] == '1') continue;
                if (ok2 && s[k] == '2') continue;
                if (ok3 && s[k] == '3') continue;
                if (ok4 && s[k] == '4') continue;
                if (ok5 && s[k] == '5') continue;
                if (ok6 && s[k] == '6') continue;
                if (ok7 && s[k] == '7') continue;
                if (ok8 && s[k] == '8') continue;
                if (ok9 && s[k] == '9') continue;


                if (zero[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("0");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (one[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("1");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (two[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("2");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (three[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("3");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (four[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("4");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (five[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("5");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (six[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("6");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (seven[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("7");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (eight[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("8");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (nine[k + 1] == true)
                {
                    var tmp = new StringBuilder();
                    tmp.Append(s[i]);
                    tmp.Append(s[k]);
                    tmp.Append("9");

                    if (!set.Contains(tmp.ToString()))
                    {
                        set.Add(tmp.ToString());
                        ans++;
                    }
                }

                if (s[k] == '0') ok0 = true;
                if (s[k] == '1') ok1 = true;
                if (s[k] == '2') ok2 = true;
                if (s[k] == '3') ok3 = true;
                if (s[k] == '4') ok4 = true;
                if (s[k] == '5') ok5 = true;
                if (s[k] == '6') ok6 = true;
                if (s[k] == '7') ok7 = true;
                if (s[k] == '8') ok8 = true;
                if (s[k] == '9') ok9 = true;
            }
        }

        // 出力
        Console.WriteLine(ans);
    }
}