int lengthOfLongestSubstring(string s) {
vector<int> dict(256, 1);
int maxLen = 0, start = 1;
for (int i = 0; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i  start);
}
return maxLen;
}
C++ code in 9 lines.

I modified a little bit to optimize the space usage:
class Solution { public: int lengthOfLongestSubstring(string s) { map<char, int> charMap; int start = 1; int maxLen = 0; for (int i = 0; i < s.size(); i++) { if (charMap.count(s[i]) != 0) { start = max(start, charMap[s[i]]); } charMap[s[i]] = i; maxLen = max(maxLen, istart); } return maxLen; } };



@smileyogurt.966 Surprisingly, yours is taking more space and time than OP's solution. Check it out!
