Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.
There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.
Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.
Input
The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.
The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.
Output
Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.
Examples
Inputcopy | Outputcopy |
---|---|
3 AaA | 2 |
Inputcopy | Outputcopy |
---|---|
7 bcAAcbc | 3 |
Inputcopy | Outputcopy |
---|---|
6 aaBCCe | 5 |
Note
In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.
In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.
In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.
给出一个长度为n的序列,求出长度最小的序列that包含序列中所有的字母(区别大小写)。详见样例
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define N 100005
#define INF INT64_MAX
#define MINF 0x3f
#define eps 1e-9
using namespace std;
int n,x,m,k,t,ans=INF,vis[N],c[N],minn=INF,maxn=-INF,flag=false;
int a[N],b[N],t1[N],t2[N],sum[N];
char s[N];
map<char,int> mp1; //map 数组作映射,记录前缀和相同的时候
map<int ,int>mp2;
void solve()
{
int tot=0;
int l=1,cnt=0,r=0;
cin>>n;
for(int i=1;i<=n;i++)
{cin>>s[i];
if(vis[s[i]]==0) tot++; //计算出不同字母的数量
vis[s[i]]++; //标记每一个已经记录过的位置
}
memset(vis,0,sizeof(vis)); //记得清空数组
while(r<n) //在右端点未到达末尾之前
{
while(cnt<tot&&r<n) //记录的字母数量要少于总数,且右端点还未到达末尾
{
r++;
if(vis[s[r]]==0) cnt++; //如果标记为0,那么就像前面一样记录数量,直到标记的数量达到tot或者到达末尾
vis[s[r]]++; //标记每一个已经记录过的位置
}
while(cnt==tot)
{
ans=min(ans,r-l+1); //计算在界域内有多少种类,并在所有的情况中取到最小值
vis[s[l]]--; // 左边界右移
if(vis[s[l]]==0) cnt--;
l++;
}
}
cout<<ans<<endl;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
solve();
return 0;
}