/*
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.
*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
unordered_map<string, int> hashTable;
int i;
int length = s.size();
int currentLength = 0;
int maxLength = 0;
i=0;
unordered_map<string, int>::iterator got;
while(i<length){
currentLength = 0;
hashTable.clear();
while(i<length && hashTable.find(s.substr(i, 1))==hashTable.end()){
hashTable.insert( {{s.substr(i,1), i}});
currentLength++;
i++;
}
if (i<length){
got = hashTable.find(s.substr(i, 1));
i = got->second+1;
}
if(currentLength > maxLength) maxLength = currentLength;
}
return maxLength;
}
};
No comments:
Post a Comment