8 Lines C++ Solution
Description\nLongest Substring Without Repeating Characters: Given a string s, find the length of the longest substring without repeating characters.\nExample:\
**Description**\n[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/): Given a string `s`, find the length of the **longest substring** without repeating characters.\nExample:\n```\nInput: s = "pwwkew"\nOutput: 3\nExplanation: The answer is "wke", with the length of 3.\nNotice that the answer must be a substring, "pwke" is a subsequence and not a substring.\n```\n\n**Solution**\nThe basic idea is to use a dictionary (called `cache`) to keep track **where the character last appeared**.\nThis is easy to see that **length = end - start**. For each character, we take its index as `end`. For `start`, we hope to use the index, all characters after it are not repeated. So, the `start` will be the maximum of `start` and `the index of the character last appeared`. Finally, we return the max length.\nThe idea is as follows:\n![image](https://assets.leetcode.com/users/images/a431245b-9c62-4d61-85d1-d0a64edb4386_1628094109.0595064.png)\n\n**Code**\n```cpp\nclass Solution {\npublic:\n    int lengthOfLongestSubstring(string s) {\n        vector<int> cache(256, -1);\n        int start = -1, maxRes = 0;\n        for(int i = 0; i < s.length(); i++) {\n            start = max(start, cache[s[i]]);\n            cache[s[i]] = i;\n            maxRes = max(maxRes, i - start);\n        }\n        return maxRes;\n    }\n};\n```\n\n**Complexity**\nTime complexity: O(n)\nSpace complexity: only use 256 array space, so the space cost is O(1)
['C']