#include <string>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
class Solution1
{
public:
int lengthOfLongestSubstring(string s)
{
int max_num = 0;
for (int i = 0; i < s.length(); i++)
{
for (int j = i + 1; j < s.length() + 1; j++)
{
if (quote(s, i, j))
{
max_num = max(max_num, j - i);
}
}
}
return max_num;
}
bool quote(string s, int i, int j)
{
map<char, int> map1;
for (int start = i; start < j; i++)
{
if (map1.find(s[start]) != map1.end())
{
return false;
}
map1[s[start]] = start;
}
return true;
}
};
class Solution2
{
public:
int lengthOfLongestSubstring(string s)
{
int start = 0;
int end = 0;
int max_num = 0;
map<char, int> hash;
int len = s.size();
while (start < len && end < len)
{
if (hash.find(s[end]) == hash.end())
{
hash[s[end++]] = end;
max_num = max(max_num, end - start);
}
else
{
hash.erase(s[start++]);
}
}
return max_num;
}
};
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int start = 0;
int end = 0;
int len = s.length();
int max_num = 0;
map<char, int> hash;
for(start,end;end<len;end++)
{
if (hash.find(s[end]) != hash.end())
{
start = max(hash.find(s[end])->second+1,start);
}
max_num=max(max_num,end-start+1);
hash[s[end]]=end;
}
return max_num;
}
};