CPP Solution for beginners | O(n) time | Longest Substring without repeating characters
A solution for beginners, which is straightforward, easy to understand, without too many complications and room to optimize once you understand the basic premis
A solution for beginners, which is straightforward, easy to understand, without too many complications and room to optimize once you understand the basic premise of the question. Hope this helps!\n\nTime Complexity: O(n)\nSpace Complexity: O(min of a,b) for the unordered set. a, is the upper bound of the space complexity.\nWhere a: Size of the string\nb: Size of the number of characters in the character-set\n\n\tclass Solution {\n\tpublic:\n\t\tint lengthOfLongestSubstring(string s) \n\t\t{\n\t\t\tunordered_set<char> set;\n        \n\t\t\tint i = 0, j = 0, n = s.size(), ans = 0;\n        \n\t\t\twhile( i<n && j<n)\n\t\t\t{\n\t\t\t\tif(set.find(s[j]) == set.end()) //If the character does not in the set\n\t\t\t\t{\n\t\t\t\t\tset.insert(s[j++]); //Insert the character in set and update the j counter\n\t\t\t\t\tans = max(ans, j-i); //Check if the new distance is longer than the current answer\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tset.erase(s[i++]); \n\t\t\t\t\t/*If character does exist in the set, ie. it is a repeated character, \n\t\t\t\t\twe update the left side counter i, and continue with the checking for substring. */\n\t\t\t\t}\n\t\t\t}\n        \n\t\t\treturn ans;\n\t\t}\n\t};
['Sliding Window', 'C++']