<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>slding window Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/slding-window/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/slding-window/</link>
	<description></description>
	<lastBuildDate>Thu, 09 Jan 2020 05:47:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>slding window Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/slding-window/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 3. Longest Substring Without Repeating Characters</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-3-longest-substring-without-repeating-characters/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-3-longest-substring-without-repeating-characters/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 12 Sep 2018 04:43:30 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[slding window]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3911</guid>

					<description><![CDATA[<p>Problem Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-3-longest-substring-without-repeating-characters/">花花酱 LeetCode 3. Longest Substring Without Repeating Characters</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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">
<iframe title="花花酱 LeetCode 3. Longest Substring Without Repeating Characters - 刷题找工作 EP295" width="500" height="281" src="https://www.youtube.com/embed/LupZFfCCbAU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>


<h1>Problem</h1>
<p>Given a string, find the length of the <b>longest substring</b> without repeating characters.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-1-1">"abcabcbb"</span>
<strong>Output: </strong><span id="example-output-1">3 
<strong>Explanation:</strong></span> The answer is <code>"abc"</code>, with the length of 3.</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-2-1">"bbbbb"</span>
<strong>Output: </strong><span id="example-output-2">1
</span><span id="example-output-1"><strong>Explanation: </strong>T</span>he answer is <code>"b"</code>, with the length of 1.</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong><span id="example-input-3-1">"pwwkew"</span>
<strong>Output: </strong><span id="example-output-3">3
</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>
<h1><strong>Solution: HashTable + Sliding Window</strong></h1>
<p>Using a hashtable to remember the last index of every char.  And keep tracking the starting point of a valid substring.</p>
<p>start = max(start, last[s[i]] + 1)</p>
<p>ans = max(ans, i &#8211; start + 1)</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(128)</p>
<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int lengthOfLongestSubstring(string s) {
    const int n = s.length();
    int ans = 0;
    vector&lt;int&gt; idx(128, -1);
    for (int i = 0, j = 0; j &lt; n; ++j) {
      i = max(i,idx[s[j]] + 1);
      ans = max(ans, j - i + 1);      
      idx[s[j]] = j;
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public int lengthOfLongestSubstring(String s) {
    int[] last = new int[128];
    Arrays.fill(last, -1);    
    int start = 0;
    int ans = 0;
    for (int i = 0; i &lt; s.length(); ++i) {      
      if (last[s.charAt(i)] != -1)
        start = Math.max(start, last[s.charAt(i)] + 1);
      last[s.charAt(i)] = i;
      ans = Math.max(ans, i - start + 1);      
    }
    return ans;
  }
}</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def lengthOfLongestSubstring(self, s):
    last = [-1] * 128
    ans = 0
    start = 0
    for i, ch in enumerate(s):
      if last[ord(ch)] != -1:
        start = max(start, last[ord(ch)] + 1)
      ans = max(ans, i - start + 1)
      last[ord(ch)] = i
    return ans</pre>
</div></div><p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-3-longest-substring-without-repeating-characters/">花花酱 LeetCode 3. Longest Substring Without Repeating Characters</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-3-longest-substring-without-repeating-characters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
