2014-10-20 11:23:39 +08:00
|
|
|
// Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
|
|
|
|
// Author : Hao Chen
|
|
|
|
// Date : 2014-07-19
|
|
|
|
|
2014-10-21 10:49:57 +08:00
|
|
|
/**********************************************************************************
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
**********************************************************************************/
|
|
|
|
|
2014-10-20 11:23:39 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int lengthOfLongestSubstring1(string s) {
|
|
|
|
map<char, int> m;
|
|
|
|
int maxLen = 0;
|
|
|
|
int lastRepeatPos = -1;
|
|
|
|
for(int i=0; i<s.size(); i++){
|
|
|
|
if (m.find(s[i])!=m.end() && lastRepeatPos < m[s[i]]) {
|
|
|
|
lastRepeatPos = m[s[i]];
|
|
|
|
}
|
|
|
|
if ( i - lastRepeatPos > maxLen ){
|
|
|
|
maxLen = i - lastRepeatPos;
|
|
|
|
}
|
|
|
|
m[s[i]] = i;
|
|
|
|
}
|
|
|
|
return maxLen;
|
|
|
|
}
|
|
|
|
//don't use <map>
|
|
|
|
int lengthOfLongestSubstring(string s) {
|
|
|
|
const int MAX_CHARS = 256;
|
|
|
|
int m[MAX_CHARS];
|
|
|
|
memset(m, -1, sizeof(m));
|
|
|
|
|
|
|
|
int maxLen = 0;
|
|
|
|
int lastRepeatPos = -1;
|
|
|
|
for(int i=0; i<s.size(); i++){
|
|
|
|
if (m[s[i]]!=-1 && lastRepeatPos < m[s[i]]) {
|
|
|
|
lastRepeatPos = m[s[i]];
|
|
|
|
}
|
|
|
|
if ( i - lastRepeatPos > maxLen ){
|
|
|
|
maxLen = i - lastRepeatPos;
|
|
|
|
}
|
|
|
|
m[s[i]] = i;
|
|
|
|
}
|
|
|
|
return maxLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
string s = "abcabcbb";
|
|
|
|
cout << s << " : " << lengthOfLongestSubstring(s) << endl;
|
|
|
|
|
|
|
|
s = "bbbbb";
|
|
|
|
cout << s << " : " << lengthOfLongestSubstring(s) << endl;
|
|
|
|
|
|
|
|
s = "bbabcdb";
|
|
|
|
cout << s << " : " << lengthOfLongestSubstring(s) << endl;
|
|
|
|
|
|
|
|
if (argc>1){
|
|
|
|
s = argv[1];
|
|
|
|
cout << s << " : " << lengthOfLongestSubstring(s) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|