<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jaden Stanton</title>
    <description>The latest articles on DEV Community by Jaden Stanton (@jadenstanton).</description>
    <link>https://dev.to/jadenstanton</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F857096%2F60086d28-fb70-4771-a938-5041175456f7.jpeg</url>
      <title>DEV Community: Jaden Stanton</title>
      <link>https://dev.to/jadenstanton</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jadenstanton"/>
    <language>en</language>
    <item>
      <title>Leetcode Problem of the Day: 567 Permutation in String</title>
      <dc:creator>Jaden Stanton</dc:creator>
      <pubDate>Thu, 22 Jun 2023 03:43:24 +0000</pubDate>
      <link>https://dev.to/jadenstanton/leetcode-problem-of-the-day-567-permutation-in-string-1ok2</link>
      <guid>https://dev.to/jadenstanton/leetcode-problem-of-the-day-567-permutation-in-string-1ok2</guid>
      <description>&lt;h4&gt;
  
  
  Welcome to the LeetCode Problem of the Day series! In today's edition, we'll be exploring question 567: &lt;a href="https://leetcode.com/problems/permutation-in-string/"&gt;Permutation in String&lt;/a&gt;. This problem challenges us to determine whether one of the permutations of a given string, s1, is a substring of another string, s2. Join me as we break down the problem, discuss the thought process, and walk through a solution step by step.
&lt;/h4&gt;




&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.&lt;/p&gt;

&lt;p&gt;In other words, return true if one of s1's permutations is the substring of s2.&lt;/p&gt;




&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Input: s1 = "ab", s2 = "eidbaooo"&lt;br&gt;
Output: true&lt;br&gt;
Explanation: s2 contains one permutation of s1 ("ba").&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;Input: s1 = "ab", s2 = "eidboaoo"&lt;br&gt;
Output: false&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 &amp;lt;= s1.length, s2.length &amp;lt;= 104
s1 and s2 consist of lowercase English letters.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  Examination
&lt;/h2&gt;

&lt;p&gt;The first thing we want to do when solving any problem is thoroughly understand what is being asked of us. What we notice when reading over this problem is that we are looking for a permutation of  &lt;code&gt;s1&lt;/code&gt; in &lt;code&gt;s2&lt;/code&gt;, now this might be tricky to wrap your head around if you are unfamiliar with permutations, but if we look at the examples we notice that the permutation is nothing more than an anagram essentially. So with that being said we are looking for any form of &lt;code&gt;s1&lt;/code&gt;in &lt;code&gt;s2&lt;/code&gt;and that can be in order or reversed, or an any order as long as the characters are consecutive.&lt;/p&gt;

&lt;p&gt;Furthermore, when we look at the constraints we learn even more, &lt;code&gt;s1 and s2 consist of lowercase English letters.&lt;/code&gt; what this means is that we don't have to worry about any special characters, numbers, or capitals, so, all we need to worry about is 26 letters.&lt;/p&gt;

&lt;p&gt;Since we are looking for a string of size &lt;code&gt;s1&lt;/code&gt; in &lt;code&gt;s2&lt;/code&gt; that has to be consecutive, we know that we can use the sliding window technique.&lt;/p&gt;

&lt;p&gt;Sure we can use a sliding window, but how can we know how far to slide it and whats in the window and whether it matches our &lt;code&gt;s1&lt;/code&gt;? Well the answer to that is to keep two arrays that keep the count of each character in &lt;code&gt;s1&lt;/code&gt; and each character in &lt;code&gt;s2&lt;/code&gt;. Once the arrays match then we know that we have found the permutation, otherwise it doesn't exist. This can be achieved by using the ascii value of each character as the index and incrementing it's count by 1 when seen.&lt;/p&gt;

&lt;p&gt;The trick is to initialize the count array of &lt;code&gt;s1&lt;/code&gt; first, since this will be the reference array that we compare the count array of &lt;code&gt;s2&lt;/code&gt; to. Then we do the same for array 2, except after every character is added to the count array, we want to check that the window is no bigger than &lt;code&gt;s1&lt;/code&gt; or that will mess us up, if it is we shrink it by one from the left. After that is done, we can check if the two arrays are equivalent, and if they are then we can return true, if not return false.&lt;/p&gt;




&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;checkInclusion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# declare variables and count arrays
&lt;/span&gt;    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# we set each array to 26 values of 0 since our constraints tell us we only need to worry about 26 characters
&lt;/span&gt;    &lt;span class="n"&gt;cnt_1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cnt_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;
    &lt;span class="c1"&gt;# Lets initialize our first count array with s1 since we will be using it as the reference
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# This gets the ascii value of the value at i in s1, uses that value as the index in the count array and increments it by 1
&lt;/span&gt;        &lt;span class="n"&gt;cnt_1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="c1"&gt;# Now we do the same for the second array
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;cnt_2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="c1"&gt;# Ensure that the window size isn't growing past the size of s1
&lt;/span&gt;        &lt;span class="c1"&gt;# What this is doing is finding the value in count array 2 that is outside of the window size that we want to maintain and removing it by decrementing by 1
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;cnt_2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="c1"&gt;# Check if the arrays match and if they do return true
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cnt_1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;cnt_2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="c1"&gt;# Other wise return False
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>tutorial</category>
      <category>python</category>
      <category>learning</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
