<?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>rolling hash Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/rolling-hash/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/rolling-hash/</link>
	<description></description>
	<lastBuildDate>Sun, 06 Feb 2022 02:21:19 +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>rolling hash Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/rolling-hash/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2156. Find Substring With Given Hash Value</title>
		<link>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2156-find-substring-with-given-hash-value/</link>
					<comments>https://zxi.mytechroad.com/blog/sliding-window/leetcode-2156-find-substring-with-given-hash-value/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Feb 2022 02:19:25 +0000</pubDate>
				<category><![CDATA[Sliding Window]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rolling hash]]></category>
		<category><![CDATA[sliding window]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9487</guid>

					<description><![CDATA[<p>The hash of a&#160;0-indexed&#160;string&#160;s&#160;of length&#160;k, given integers&#160;p&#160;and&#160;m, is computed using the following function: hash(s, p, m) = (val(s[0]) * p0&#160;+ val(s[1]) * p1&#160;+ ... +&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-2156-find-substring-with-given-hash-value/">花花酱 LeetCode 2156. Find Substring With Given Hash Value</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The hash of a&nbsp;<strong>0-indexed</strong>&nbsp;string&nbsp;<code>s</code>&nbsp;of length&nbsp;<code>k</code>, given integers&nbsp;<code>p</code>&nbsp;and&nbsp;<code>m</code>, is computed using the following function:</p>



<ul><li><code>hash(s, p, m) = (val(s[0]) * p<sup>0</sup>&nbsp;+ val(s[1]) * p<sup>1</sup>&nbsp;+ ... + val(s[k-1]) * p<sup>k-1</sup>) mod m</code>.</li></ul>



<p>Where&nbsp;<code>val(s[i])</code>&nbsp;represents the index of&nbsp;<code>s[i]</code>&nbsp;in the alphabet from&nbsp;<code>val('a') = 1</code>&nbsp;to&nbsp;<code>val('z') = 26</code>.</p>



<p>You are given a string&nbsp;<code>s</code>&nbsp;and the integers&nbsp;<code>power</code>,&nbsp;<code>modulo</code>,&nbsp;<code>k</code>, and&nbsp;<code>hashValue.</code>&nbsp;Return&nbsp;<code>sub</code>,<em>&nbsp;the&nbsp;<strong>first</strong>&nbsp;<strong>substring</strong>&nbsp;of&nbsp;</em><code>s</code><em>&nbsp;of length&nbsp;</em><code>k</code><em>&nbsp;such that&nbsp;</em><code>hash(sub, power, modulo) == hashValue</code>.</p>



<p>The test cases will be generated such that an answer always&nbsp;<strong>exists</strong>.</p>



<p>A&nbsp;<strong>substring</strong>&nbsp;is a contiguous non-empty sequence of characters within a string.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
<strong>Output:</strong> "ee"
<strong>Explanation:</strong> The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0. 
"ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
<strong>Output:</strong> "fbx"
<strong>Explanation:</strong> The hash of "fbx" can be computed to be hash("fbx", 31, 100) = (6 * 1 + 2 * 31 + 24 * 31<sup>2</sup>) mod 100 = 23132 mod 100 = 32. 
The hash of "bxz" can be computed to be hash("bxz", 31, 100) = (2 * 1 + 24 * 31 + 26 * 31<sup>2</sup>) mod 100 = 25732 mod 100 = 32. 
"fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
Note that "bxz" also has a hash of 32 but it appears later than "fbx".
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= k &lt;= s.length &lt;= 2 * 10<sup>4</sup></code></li><li><code>1 &lt;= power, modulo &lt;= 10<sup>9</sup></code></li><li><code>0 &lt;= hashValue &lt; modulo</code></li><li><code>s</code>&nbsp;consists of lowercase English letters only.</li><li>The test cases are generated such that an answer always&nbsp;<strong>exists</strong>.</li></ul>



<h2><strong>Solution: Sliding window</strong></h2>



<p>hash = (((<meta charset="utf-8">hash &#8211; (s[i+k] * p<sup>k-1</sup>) % mod + mod) * p) + (s[i] * p<sup>0</sup>)) % mod</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string subStrHash(string s, int power, int modulo, int k, int hashValue) {    
    const int n = s.length();
    long p = 1; 
    for (int i = 1; i &lt; k; ++i)
      p = (p * power) % modulo;
    long ans = 0;
    for (long i = n - 1, cur = 0; i &gt;= 0; --i) {
      if (i + k &lt; n) 
        cur = ((cur - (s[i + k] - 'a' + 1) * p) % modulo + modulo) % modulo;
      cur = (cur * power + (s[i] - 'a' + 1)) % modulo;      
      if (i + k &lt;= n &amp;&amp; cur == hashValue) 
        ans = i;
    }    
    return s.substr(ans, k);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sliding-window/leetcode-2156-find-substring-with-given-hash-value/">花花酱 LeetCode 2156. Find Substring With Given Hash Value</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/sliding-window/leetcode-2156-find-substring-with-given-hash-value/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1392. Longest Happy Prefix</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1392-longest-happy-prefix/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1392-longest-happy-prefix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 05:37:00 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[kmp]]></category>
		<category><![CDATA[rolling hash]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6523</guid>

					<description><![CDATA[<p>A string is called a&#160;happy prefix&#160;if is a&#160;non-empty&#160;prefix which is also a suffix (excluding itself). Given a string&#160;s. Return the&#160;longest happy prefix&#160;of&#160;s&#160;. Return an empty&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1392-longest-happy-prefix/">花花酱 LeetCode 1392. Longest Happy Prefix</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A string is called a&nbsp;<em>happy prefix</em>&nbsp;if is a&nbsp;<strong>non-empty</strong>&nbsp;prefix which is also a suffix (excluding itself).</p>



<p>Given a string&nbsp;<code>s</code>. Return the&nbsp;<strong>longest happy prefix</strong>&nbsp;of&nbsp;<code>s</code>&nbsp;.</p>



<p>Return an empty string if no such prefix exists.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "level"
<strong>Output:</strong> "l"
<strong>Explanation:</strong> s contains 4 prefix excluding itself ("l", "le", "lev", "leve"), and suffix ("l", "el", "vel", "evel"). The largest prefix which is also suffix is given by "l".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "ababab"
<strong>Output:</strong> "abab"
<strong>Explanation:</strong> "abab" is the largest prefix which is also suffix. They can overlap in the original string.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "leetcodeleet"
<strong>Output:</strong> "leet"
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "a"
<strong>Output:</strong> ""
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length &lt;= 10^5</code></li><li><code>s</code>&nbsp;contains only lowercase English letters.</li></ul>



<h2><strong>Solution: Rolling Hash</strong></h2>



<p>Time complexity: O(n) / worst case: O(n^2)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string longestPrefix(string_view s) {
    const int kMod = 16769023;
    const int kBase = 128;
    const int n = s.length();
    int p = 0;
    int q = 0;
    int a = 1;
    int ans = 0;
    for (int i = 1; i &lt; n; ++i) {
      p = (p + a * s[i - 1]) % kMod;
      q = (q * kBase + s[n - i]) % kMod;      
      a = (a * kBase) % kMod;
      if (p == q &amp;&amp; s.substr(0, i) == s.substr(n - i))
        ans = i;
    }
    return string(s.substr(0, ans));
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1392-longest-happy-prefix/">花花酱 LeetCode 1392. Longest Happy Prefix</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/string/leetcode-1392-longest-happy-prefix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
