class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len = 0;
unordered_map<char,int> mp;
int start=0;
int end = start;
while(end<s.size())
{
if (mp.find(s[end])!=mp.end())
{
start = max(start,mp[s[end]]+1);
mp[s[end]]=end;
}
else
{
mp[s[end]]=end;
}
len = max(len,end-start+1);
end++;
}
return len;
}
};
Thursday, March 5, 2015
3 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment