首先理解子串和子序列的区别,然后利用滑动窗口解决该问题,再者利用哈希表减小空间存储。
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer>map=new HashMap<>();
int first=0;
int result=0;
for(int last=0;last<s.length();last++)
{
if(map.containsKey(s.charAt(last)))
first=Math.max(map.get(s.charAt(last)),first);
result=Math.max(result,last-first+1);
map.put(s.charAt(last),last+1);
}
return result;
}
}