Given a string, find the length of the longest substring without repeating characters. For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA” and “DEFGAB”, with length 6.
Example:
Input:
2
geeksforgeeks
qwertqwer
Output:
7
5
Given a string, find the length of the longest substring without repeating characters. |
#include <bits/stdc++.h>
using namespace std;
int main() {
//code
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int a[26];
int i = 0, j = 0, l = 0;
memset(a, 0, sizeof(a));
while (j < s.size()) {
while (i < s.size()) {
a[s[i] - 'a'] += 1;
if (a[s[i] - 'a'] >= 2) {
l = max(l, i - j);
memset(a, 0, sizeof(a));
break;
}
++i;
}
l = max(l, i - j);
++j;
i = j;
}
cout << l << endl;
}
return 0;
}