My O(n) Solution
if only use DP, it's an O(n*n) solution, adding a map to get O(n).\n    \n    class Solution {\n        public:\n            int lengthOfLongestSubstring(string
if only use DP, it's an O(n*n) solution, adding a map to get O(n).\n    \n    class Solution {\n        public:\n            int lengthOfLongestSubstring(string s) {\n                if(s.size()<2) return s.size();\n                int d=1, maxLen=1;\n                unordered_map<char,int> map;\n                map[s[0]]=0;\n                for(int i=1;i<s.size();i++)\n                {\n                    if(map.count(s[i])==0 || map[s[i]]<i-d)\n                        d++;\n                    else\n                        d= i- map[s[i]];\n                    map[s[i]]=i;\n                    if(d>maxLen)\n                        maxLen = d;\n                }\n                return maxLen;\n            }\n        };
[]