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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#define NDEBUG
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

// for DEBUG {{{
#ifdef NDEBUG
#define DEBUG_PRINT(fmt, ...) ((void)0)
#else
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
#endif
#define ASSERT_RANGE(min,x,max) assert(((x)>=(min)) && ((x)<=(max)))
// }}}

int sort_int(int *a, int N) {// {{{
	int i;
	for (i = 0; i < N-1; i++) {
		int j;
		for (j = i + 1; j < N; j++) {
			if (a[i] > a[j]) {
				int tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
	}
	return 0;
}// }}}
void merge_int(int *a, int *b, int lower, int sep, int upper) {// {{{
	int i_lower = lower;
	int i_upper = sep;
	int i_b = 0;
	while ((i_lower < sep) && (i_upper < upper)) {
		if (a[i_lower] < a[i_upper]) {
			b[i_b++] = a[i_lower++];
		} else {
			b[i_b++] = a[i_upper++];
		}
	}
	if (i_lower >= sep) {
		while (i_upper < upper) {
			b[i_b++] = a[i_upper++];
		}
	} else {
		while (i_lower < sep) {
			b[i_b++] = a[i_lower++];
		}
	}
	for (i_lower = 0; i_lower < i_b; i_lower++) {
		a[i_lower+lower] = b[i_lower];
	}
}// }}}
void merge_sort_int(int *a, int *b, int lower, int upper) {// {{{
	int sep;
	if ((lower == upper) || (lower + 1 == upper)) {
		return;
	}
	sep = (lower + upper) / 2;
	merge_sort_int(a,b,lower,sep);
	merge_sort_int(a,b,sep,upper);
	merge_int(a,b,lower,sep,upper);
}// }}}

#define S_MAX 30000
int main(int argc, char **argv) {
	int n;
	char s[S_MAX+1];
	fscanf(stdin,"%d",&n) > 0;
	fscanf(stdin,"%s",s) > 0;
	assert(strlen(s) == n);

	int init_index[10];
	int end_index[10];
	{
		int i;
		for (i = 0; i < 10; i++) {
			init_index[i] = -1;
			end_index[i] = -1;
		}
	}
	{
		int i;
		for (i = 0; i < n; i++) {
			int index = (s[i] - '0');
			end_index[index] = i;
			if (init_index[index] < 0) {
				init_index[index] = i;
			}
		}
	}
#ifndef NDEBUG
	{
		int i;
		for (i = 0; i < 10; i++) {
			fprintf(stderr,"%d %d %d\n", i, init_index[i],end_index[i]);
		}
	}
#endif
	int ret = 0;
	{
		int i;
		for (i = 0; i < 10; i++) {
			int init = init_index[i];
			if (init < 0) {
				continue;
			}
			int j;
			for (j = 0; j < 10; j++) {
				int end = end_index[j];
				if (init >= end) {
					continue;
				}
				DEBUG_PRINT("%d %d %d %d\n",i,j,init,end);
				int mid_index[10];
				memset(mid_index,0,sizeof(int)*10);
				{
					int k;
					for (k = init+1; k < end; k++) {
						int index = (s[k] - '0');
						mid_index[index] = 1;
					}
				}
				{
					int k;
					for (k = 0; k < 10; k++) {
						ret += mid_index[k];
					}
				}
			}
		}
	}
	fprintf(stdout,"%d\n",ret);
	return 0;
}