Longest Substring Without Repeating Characters Solutions in C++
Number 3
Difficulty Medium
Acceptance 30.4%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/// Author : Hao Chen// Date : 2014-07-19#include <string.h>#include <iostream>#include <string>#include <map>using namespace std;/** Idea:** Using a map store each char's index.** So, we can be easy to know the when duplication and the previous duplicated char's index.** Then we can take out the previous duplicated char, and keep tracking the maxiumn length.**/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;}
C++ solution by pezy/LeetCode
#include <string>using std::string;#include <unordered_map>using std::unordered_map;#include <algorithm>using std::max;class Solution {public:int lengthOfLongestSubstring(string s) {size_t ret = 0, start = 0;unordered_map<char, size_t> trace;for (size_t i = 0; i < s.size(); ++i) {auto found = trace.find(s[i]);if (found != trace.end() && found->second >= start) {ret = max(ret, i - start);start = found->second + 1;}trace[s[i]] = i;}return max(ret, s.size() - start);}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-substring-without-repeating-characters/description//// Author : liuyubobobo/// Time : 2017-11-14#include <iostream>#include <string>#include <cassert>using namespace std;// Sliding Window// Time Complexity: O(len(s))// Space Complexity: O(len(charset))class Solution {public:int lengthOfLongestSubstring(string s) {int freq[256] = {0};int l = 0, r = -1; // sliding window: s[l...r]int res = 0;while(r + 1 < s.size()){if( freq[s[r + 1]] == 0 )freq[s[++r]] ++;else //freq[s[r+1]] == 1freq[s[l++]] --;res = max(res, r - l + 1);}return res;}};int main() {cout << Solution().lengthOfLongestSubstring( "abcabcbb" )<<endl; //3cout << Solution().lengthOfLongestSubstring( "bbbbb" )<<endl; //1cout << Solution().lengthOfLongestSubstring( "pwwkew" )<<endl; //3cout << Solution().lengthOfLongestSubstring( "c" )<<endl; //1cout << Solution().lengthOfLongestSubstring( "" )<<endl; //0return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-substring-without-repeating-characters/description//// Author : liuyubobobo/// Time : 2017-11-14#include <iostream>#include <string>#include <cassert>using namespace std;// Sliding Window// Another Implementation//// Time Complexity: O(len(s))// Space Complexity: O(len(charset))class Solution {public:int lengthOfLongestSubstring(string s) {int freq[256] = {0};int l = 0, r = -1; // sliding window: s[l...r]int res = 0;while(r + 1 < s.size()){while(r + 1 < s.size() && freq[s[r + 1]] == 0)freq[s[++r]] ++;res = max(res, r - l + 1);if(r + 1 < s.size()){freq[s[++r]] ++;assert(freq[s[r]] == 2);while(l <= r && freq[s[r]] == 2)freq[s[l++]] --;}}return res;}};int main() {cout << Solution().lengthOfLongestSubstring( "abcabcbb" )<<endl; //3cout << Solution().lengthOfLongestSubstring( "bbbbb" )<<endl; //1cout << Solution().lengthOfLongestSubstring( "pwwkew" )<<endl; //3cout << Solution().lengthOfLongestSubstring( "c" )<<endl; //1cout << Solution().lengthOfLongestSubstring( "" )<<endl; //0return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-substring-without-repeating-characters/description//// Author : liuyubobobo/// Time : 2017-11-14#include <iostream>#include <string>#include <cassert>using namespace std;// Sliding Window// Using last[c] to record the last occurance of character c// Using last[c] to quickly move the pointer l// only r moved from left to right, Using n times calculation of res instead of 2*n times//// Time Complexity: O(len(s))// Space Complexity: O(len(charset))class Solution {public:int lengthOfLongestSubstring(string s) {int last[256];memset(last, -1, sizeof(last));int l = 0, r = -1; // sliding window: s[l...r]int res = 0;while(r + 1 < s.size()){r ++;if(last[s[r]] != -1)l = max(l, last[s[r]] + 1);res = max(res, r - l + 1);last[s[r]] = r;}return res;}};int main() {cout << Solution().lengthOfLongestSubstring( "abcabcbb" )<<endl; //3cout << Solution().lengthOfLongestSubstring( "bbbbb" )<<endl; //1cout << Solution().lengthOfLongestSubstring( "pwwkew" )<<endl; //3cout << Solution().lengthOfLongestSubstring( "c" )<<endl; //1cout << Solution().lengthOfLongestSubstring( "abba" )<<endl; //2cout << Solution().lengthOfLongestSubstring( "" )<<endl; //0return 0;}