Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirainmoe/a39a65e73efc95012bb7be84d32337c7 to your computer and use it in GitHub Desktop.
Save kirainmoe/a39a65e73efc95012bb7be84d32337c7 to your computer and use it in GitHub Desktop.
class Solution {
public:
inline int lengthOfLongestSubstring(string s) {
const int maxs = 96;
int pos[maxs];
int ans = 0;
int cur = 0;
int tmp;
int length = s.length();
int flag = length;
memset(pos, -1, sizeof(pos));
for (int i = length - 1; i >= 0; i --)
{
tmp = s[i] - 32;
if (pos[tmp] != -1)
{
if (pos[tmp] > flag) {
cur = flag - i;
ans = max(cur, ans);
} else {
flag = pos[tmp];
cur = flag - i;
}
} else {
cur += 1;
ans = max(cur, ans);
}
pos[tmp] = i;
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment