{"id":3911,"date":"2018-09-11T21:43:30","date_gmt":"2018-09-12T04:43:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3911"},"modified":"2020-01-08T21:47:24","modified_gmt":"2020-01-09T05:47:24","slug":"leetcode-3-longest-substring-without-repeating-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-3-longest-substring-without-repeating-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3. Longest Substring Without Repeating Characters"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 3. Longest Substring Without Repeating Characters - \u5237\u9898\u627e\u5de5\u4f5c EP295\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/LupZFfCCbAU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n<h1>Problem<\/h1>\n<p>Given a string, find the length of the\u00a0<b>longest substring<\/b>\u00a0without repeating characters.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"abcabcbb\"<\/span>\n<strong>Output: <\/strong><span id=\"example-output-1\">3 \n<strong>Explanation:<\/strong><\/span> The answer is <code>\"abc\"<\/code>, with the length of 3.<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">\"bbbbb\"<\/span>\n<strong>Output: <\/strong><span id=\"example-output-2\">1\n<\/span><span id=\"example-output-1\"><strong>Explanation: <\/strong>T<\/span>he answer is <code>\"b\"<\/code>, with the length of 1.<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">\"pwwkew\"<\/span>\n<strong>Output: <\/strong><span id=\"example-output-3\">3\n<\/span><span id=\"example-output-1\"><strong>Explanation: <\/strong><\/span>The answer is <code>\"wke\"<\/code>, with the length of 3. Note that the answer must be a <b>substring<\/b>, <code>\"pwke\"<\/code> is a <i>subsequence<\/i> and not a substring.<\/pre>\n<h1><strong>Solution: HashTable + Sliding Window<\/strong><\/h1>\n<p>Using a hashtable to remember the last index of every char.\u00a0 And keep tracking the starting point of a valid substring.<\/p>\n<p>start = max(start, last[s[i]] + 1)<\/p>\n<p>ans = max(ans, i &#8211; start + 1)<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(128)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int lengthOfLongestSubstring(string s) {\n    const int n = s.length();\n    int ans = 0;\n    vector<int> idx(128, -1);\n    for (int i = 0, j = 0; j < n; ++j) {\n      i = max(i,idx[s[j]] + 1);\n      ans = max(ans, j - i + 1);      \n      idx[s[j]] = j;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\nclass Solution {\n  public int lengthOfLongestSubstring(String s) {\n    int[] last = new int[128];\n    Arrays.fill(last, -1);    \n    int start = 0;\n    int ans = 0;\n    for (int i = 0; i &lt; s.length(); ++i) {      \n      if (last[s.charAt(i)] != -1)\n        start = Math.max(start, last[s.charAt(i)] + 1);\n      last[s.charAt(i)] = i;\n      ans = Math.max(ans, i - start + 1);      \n    }\n    return ans;\n  }\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua\nclass Solution:\n  def lengthOfLongestSubstring(self, s):\n    last = [-1] * 128\n    ans = 0\n    start = 0\n    for i, ch in enumerate(s):\n      if last[ord(ch)] != -1:\n        start = max(start, last[ord(ch)] + 1)\n      ans = max(ans, i - start + 1)\n      last[ord(ch)] = i\n    return ans\n<\/pre>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Problem Given a string, find the length of the\u00a0longest substring\u00a0without repeating characters. Example 1: Input: &#8220;abcabcbb&#8221; Output: 3 Explanation: The answer is &#8220;abc&#8221;, with the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[177,399,4],"class_list":["post-3911","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-medium","tag-slding-window","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3911"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3911\/revisions"}],"predecessor-version":[{"id":6062,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3911\/revisions\/6062"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}