// Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ // Author : Hao Chen // Date : 2014-07-19 #include #include #include #include using namespace std; int lengthOfLongestSubstring1(string s) { map m; int maxLen = 0; int lastRepeatPos = -1; for(int i=0; i maxLen ){ maxLen = i - lastRepeatPos; } m[s[i]] = i; } return maxLen; } //don't use 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 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; }