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 PHP7 (7.0.15) (judge verdict: Accepted)

<?php
$n = (int)trim(fgets(STDIN));
$s = trim(fgets(STDIN));
$map = [];


$csum = [
	0 => [],
	1 => [],
	2 => [],
	3 => [],
	4 => [],
	5 => [],
	6 => [],
	7 => [],
	8 => [],
	9 => [],
];

for ($i=0; $i < $n; $i++) { 
	$char = $s[$i];
	foreach ($csum as $num => &$array) {
		if ($i === 0) {
			$array[$i] = 0;
		}else{
			$array[$i] = $array[$i-1];
		}
	}
	$csum[$char][$i] += 1;
}

// $sum = 0;
//2文字目固定
for ($i=1; $i < $n-1; $i++) { 
	//1文字目の選択肢
	foreach ($csum as $num1 => $array1) {
		//3文字目の選択肢
		foreach ($csum as $num2 => $array2) {
			if (isset($map[$num1][$s[$i]][$num2])) {
				continue;
			}

			if ($array1[$i-1] > 0 && $array2[$n-1] - $array2[$i] > 0) {
				// $sum += $array1[$i-1] * ($array2[$n-1] - $array2[$i]);
				$map[$num1][$s[$i]][$num2] = 1;
			}
		}
	}
}
$sum = 0;
foreach ($map as $first => $seconds) {
	foreach ($seconds as $second => $thirds) {
		$sum += count($thirds);
	}
}
// var_dump($map);
echo $sum;

//1文字目
// for ($i=0; $i < $n-2; $i++) { 
// 	//2文字目
// 	for ($j=$i+1; $j < $n-1; $j++) { 
// 		// 1-2桁目の組み合わせが出現済みならスキップ
// 		if (isset($map[$s[$i] . $s[$j]])) {
// 			continue;
// 		}
// 		//3文字目の選択肢はいくつあるか？
// 		$selectable = 0;
// 		foreach ($csum as $num => $array) {
// 			if ($array[$n] - $array[$j] > 0) {
// 				$selectable += 1;
// 			}
// 		}
// 		$map[$s[$i] . $s[$j]] = $selectable;
// 	}
// }
// //ksort($map);
// //var_dump($map);
// $sum = 0;
// foreach ($map as $k => $v) {
// 	$sum += $v;
// }
// echo $sum;