<?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>shift Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/shift/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/shift/</link>
	<description></description>
	<lastBuildDate>Sun, 09 Aug 2020 16:25:57 +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>shift Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/shift/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1540. Can Convert String in K Moves</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1540-can-convert-string-in-k-moves/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1540-can-convert-string-in-k-moves/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 09 Aug 2020 16:24:37 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[shift]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7216</guid>

					<description><![CDATA[<p>Given two strings&#160;s&#160;and&#160;t, your goal is to convert&#160;s&#160;into&#160;t&#160;in&#160;kmoves or less. During the&#160;ith&#160;(1 &#60;= i &#60;= k)&#160;move you can: Choose any index&#160;j&#160;(1-indexed) from&#160;s, such that&#160;1 &#60;=&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1540-can-convert-string-in-k-moves/">花花酱 LeetCode 1540. Can Convert String in K Moves</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>Given two strings&nbsp;<code>s</code>&nbsp;and&nbsp;<code>t</code>, your goal is to convert&nbsp;<code>s</code>&nbsp;into&nbsp;<code>t</code>&nbsp;in&nbsp;<code>k</code>moves or less.</p>



<p>During the&nbsp;<code>i<sup>th</sup></code>&nbsp;(<code>1 &lt;= i &lt;= k</code>)&nbsp;move you can:</p>



<ul><li>Choose any index&nbsp;<code>j</code>&nbsp;(1-indexed) from&nbsp;<code>s</code>, such that&nbsp;<code>1 &lt;= j &lt;= s.length</code>&nbsp;and&nbsp;<code>j</code>&nbsp;has not been chosen in any previous move,&nbsp;and shift the character at that index&nbsp;<code>i</code>&nbsp;times.</li><li>Do nothing.</li></ul>



<p>Shifting a character means replacing it by the next letter in the alphabet&nbsp;(wrapping around so that&nbsp;<code>'z'</code>&nbsp;becomes&nbsp;<code>'a'</code>). Shifting a character by&nbsp;<code>i</code>&nbsp;means applying the shift operations&nbsp;<code>i</code>&nbsp;times.</p>



<p>Remember that any index&nbsp;<code>j</code>&nbsp;can be picked at most once.</p>



<p>Return&nbsp;<code>true</code>&nbsp;if it&#8217;s possible to convert&nbsp;<code>s</code>&nbsp;into&nbsp;<code>t</code>&nbsp;in no more than&nbsp;<code>k</code>&nbsp;moves, otherwise return&nbsp;<code>false</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "input", t = "ouput", k = 9
<strong>Output:</strong> true
<strong>Explanation: </strong>In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "abc", t = "bcd", k = 10
<strong>Output:</strong> false
<strong>Explanation: </strong>We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aab", t = "bbb", k = 27
<strong>Output:</strong> true
<strong>Explanation: </strong>In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.
</pre>



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



<ul><li><code>1 &lt;= s.length, t.length &lt;= 10^5</code></li><li><code>0 &lt;= k &lt;= 10^9</code></li><li><code>s</code>,&nbsp;<code>t</code>&nbsp;contain&nbsp;only lowercase English letters.</li></ul>



<p><strong>Solution: HashTable</strong></p>



<p>Count how many times a d-shift has occurred. <br>a -> c is a 2-shift, z -> b is also 2-shift<br>a -> d is a 3-shift<br>a -> a is a 0-shift that we can skip<br>if a d-shift happened for the first time, we need at least d moves<br>However, if it happened for c times, we need at least d + 26 * c moves<br>e.g. we can do a 2-shift at the 2nd move, do another one at 2 + 26 = 28th move and do another at 2 + 26*2 = 54th move, and so on.<br>Need to find maximum move we need and make sure that one is &lt;= k.<br>Since we can pick any index to shift, so the order doesn&#8217;t matter. We can start from left to right.</p>



<p>Time complexity: O(n)<br>Space complexity: O(26) = 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:
  bool canConvertString(string s, string t, int k) {
    if (s.length() != t.length()) return false;
    vector&lt;int&gt; count(26);    
    for (int i = 0; i &lt; s.length(); ++i) {            
      int d = (t[i] - s[i] + 26) % 26;
      int c = count[d]++;
      if (d != 0 &amp;&amp; d + c * 26 &gt; k)
        return false;
    }    
    return true;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1540-can-convert-string-in-k-moves/">花花酱 LeetCode 1540. Can Convert String in K Moves</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-1540-can-convert-string-in-k-moves/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 848. Shifting Letters</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-848-shifting-letters/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-848-shifting-letters/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 12 Jun 2018 05:52:36 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[shift]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2914</guid>

					<description><![CDATA[<p>Problem We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a').&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-848-shifting-letters/">花花酱 LeetCode 848. Shifting Letters</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><iframe width="500" height="375" src="https://www.youtube.com/embed/gOycoA8pOqg?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>We have a string <code>S</code> of lowercase letters, and an integer array <code>shifts</code>.</p>
<p>Call the <em>shift</em> of a letter, the next letter in the alphabet, (wrapping around so that <code>'z'</code> becomes <code>'a'</code>).</p>
<p>For example, <code>shift('a') = 'b'</code>, <code>shift('t') = 'u'</code>, and <code>shift('z') = 'a'</code>.</p>
<p>Now for each <code>shifts[i] = x</code>, we want to shift the first <code>i+1</code> letters of <code>S</code>, <code>x</code> times.</p>
<p>Return the final string after all such shifts to <code>S</code> are applied.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>S = "abc", shifts = [3,5,9]
<strong>Output: </strong>"rpl"
<strong>Explanation: </strong>
We start with "abc".
After shifting the first 1 letters of S by 3, we have "dbc".
After shifting the first 2 letters of S by 5, we have "igc".
After shifting the first 3 letters of S by 9, we have "rpl", the answer.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= S.length = shifts.length &lt;= 20000</code></li>
<li><code>0 &lt;= shifts[i] &lt;= 10 ^ 9</code></li>
</ol>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 54 ms
class Solution {
public:
  string shiftingLetters(string S, vector&lt;int&gt;&amp; shifts) {
    int c = 0;
    for (int i = shifts.size() - 1; i &gt;= 0; --i) {
      c += (shifts[i] % 26);
      S[i] = (S[i] - 'a' + c) % 26 + 'a';
    }          
    return S;
  }
};</pre><p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-848-shifting-letters/">花花酱 LeetCode 848. Shifting Letters</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-848-shifting-letters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
