int main()
{
char str[] = "Rajat Grover is a good boy";
char set[] = "aio";
int lenStr = strlen(str);
int lenSet = strlen(set);
int minDist = 32767; //Some very large value
int maxInd = lenStr;
int minInd = -1;
bool allFound = false;
char *hashSet = new char[lenSet];
for(int i=0; i<lenSet; hashSet[i++] = -1);
for(i=0; i<lenStr; ++i)
{
int ind = -1;
if(-1 != (ind = Index(set, str[i], lenSet)))
{
hashSet[ind] = i;
if(!allFound)
{
allFound = true;
for(int j=0; j<lenSet; ++j)
{
if(hashSet[j] == -1)
{
allFound = false;
break;
}
}
if(!allFound)
continue;
}
int tempMax, tempMin;
int tempDistance = (tempMax = Max(hashSet,lenSet)) - (tempMin =
Min(hashSet,lenSet));
if(minDist > ++tempDistance)
{
maxInd = tempMax;
minInd = tempMin;
if((minDist = tempDistance) == lenSet)
break;
}
}
}
cout<<"Minimum Interval: "<<minDist<<". Between index "<<minInd
<<" and "<<maxInd<<'\n';
return 0;
}