<?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>surffix Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/surffix/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/surffix/</link>
	<description></description>
	<lastBuildDate>Tue, 06 Feb 2024 06:53:11 +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>surffix Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/surffix/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 3029. Minimum Time to Revert Word to Initial State I</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-3029-minimum-time-to-revert-word-to-initial-state-i/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-3029-minimum-time-to-revert-word-to-initial-state-i/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Feb 2024 02:02:17 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[surffix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10114</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;string&#160;word&#160;and an integer&#160;k. At every second, you must perform the following operations: Remove the first&#160;k&#160;characters of&#160;word. Add any&#160;k&#160;characters to the end of&#160;word.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-3029-minimum-time-to-revert-word-to-initial-state-i/">花花酱 LeetCode 3029. Minimum Time to Revert Word to Initial State I</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;string&nbsp;<code>word</code>&nbsp;and an integer&nbsp;<code>k</code>.</p>



<p>At every second, you must perform the following operations:</p>



<ul><li>Remove the first&nbsp;<code>k</code>&nbsp;characters of&nbsp;<code>word</code>.</li><li>Add any&nbsp;<code>k</code>&nbsp;characters to the end of&nbsp;<code>word</code>.</li></ul>



<p><strong>Note</strong>&nbsp;that you do not necessarily need to add the same characters that you removed. However, you must perform&nbsp;<strong>both</strong>&nbsp;operations at every second.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;time greater than zero required for</em>&nbsp;<code>word</code>&nbsp;<em>to revert to its&nbsp;<strong>initial</strong>&nbsp;state</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "abacaba", k = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "abacaba", k = 4
<strong>Output:</strong> 1
<strong>Explanation:</strong> At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "abcbabcd", k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
</pre>



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



<ul><li><code>1 &lt;= word.length &lt;= 50</code></li><li><code>1 &lt;= k &lt;= word.length</code></li><li><code>word</code>&nbsp;consists only of lowercase English letters.</li></ul>



<h2><strong>Solution: Suffix ==? Prefix</strong></h2>



<p>Compare the suffix with prefix.</p>



<p>word = &#8220;abacaba&#8221;, k = 3<br>ans = 1, &#8220;<s>aba</s>caba&#8221; != &#8220;abac<s>aba</s>&#8220;<br>ans = 2, &#8220;<s>abacab</s>a&#8221; == &#8220;a<s>bacaba</s>&#8220;, we find it.</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int minimumTimeToInitialState(string_view word, int k) {
    const int n = word.length();
    for (int i = 1; i * k &lt; n; ++i) {
      string_view t = word.substr(i * k);
      if (t == word.substr(0, t.length())) return i;
    }      
    return (n + k - 1) / k;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-3029-minimum-time-to-revert-word-to-initial-state-i/">花花酱 LeetCode 3029. Minimum Time to Revert Word to Initial State I</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-3029-minimum-time-to-revert-word-to-initial-state-i/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
