how is the contents of
int lastIndex[NO_OF_CHARS]={-1};
different from the contents of
vector<int> lastIndex(NO_OF_CHARS, -1);
?
How I use it (copied from comments on @digital_hog's answer):
#define NO_OF_CHARS 256
int longestUniqueSubsttr(string str) {
int n = str.size();
int res = 0;
vector<int> lastIndex(NO_OF_CHARS,-1); // i want to replace this with array
int i = 0;
for (int j = 0; j < n; j++) {
i = max(i, lastIndex[str[j]] + 1);
res = max(res, j - i + 1); lastIndex[str[j]] = j;
}
return res;
}
int main() {
int t;
cin>>t;
while(t--) {
string s="";
cin>>s;
cout<<longestUniqueSubsttr(s)<<endl;
}
return 0;
}