<?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: Sarunas Medeikis</title>
    <description>The latest articles on DEV Community by Sarunas Medeikis (@sarunasmedeikis).</description>
    <link>https://dev.to/sarunasmedeikis</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%2F204953%2F7ace271b-51ae-4cf8-a726-778f0138e4d1.jpg</url>
      <title>DEV Community: Sarunas Medeikis</title>
      <link>https://dev.to/sarunasmedeikis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sarunasmedeikis"/>
    <language>en</language>
    <item>
      <title>In search of subsequence</title>
      <dc:creator>Sarunas Medeikis</dc:creator>
      <pubDate>Fri, 27 Jan 2023 22:52:33 +0000</pubDate>
      <link>https://dev.to/sarunasmedeikis/in-search-of-subsequence-64e</link>
      <guid>https://dev.to/sarunasmedeikis/in-search-of-subsequence-64e</guid>
      <description>&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@jjying?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;JJ Ying&lt;/a&gt; on &lt;a href="https://unsplash.com/wallpapers/cool/abstract?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Consider that you need to find whether two strings are subsequent to each other.&lt;/p&gt;

&lt;p&gt;If A is subsequent to B, return &lt;strong&gt;true&lt;/strong&gt;, if not - &lt;strong&gt;false&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But to begin with.. What is 'subsequence'?&lt;br&gt;
Imagine there is a list (T), a subsequence is a part of that list, but in a certain order(S).&lt;br&gt;
For example, we have a list (T), which consists of letters "ABCD" and we want to find a subsequence, let's pick some letters from our list (T), like "AC" and make a new list just with those letters "AC", that will be our new list (S). To make S a subsequence of T it must have the same order. Check image below, for visual. in our S list we have A and C, and in our T list, A comes first, just like in our S list, also C comes after that. So our S is a subsequence of T.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5x6i0bmt2btjpwizn4ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5x6i0bmt2btjpwizn4ts.png" alt="A subsequence" width="541" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, when we know what IS a subsequence, let's put it in ink about what is NOT a subsequence.&lt;/p&gt;

&lt;p&gt;In the image below, everything is essentially the same, except inside our S list I performed a switcheroo, and switched A &amp;lt;-&amp;gt; C , and suddenly, our S is not a subsequence of T anymore.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vfeliwf2f7tg2ddyw1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vfeliwf2f7tg2ddyw1f.png" alt="Not a subsequence anymore" width="535" height="167"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another important thing to remember, that for S to be a subsequence of T, they don't have to be consecutive strings, only order has to stay. Meaning that if we take our example from above, and update our T list by adding "CA" at the end, then our S would be a subsequence of T. Check image below, for visualisation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4lldydkpdbqzbcoahne6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4lldydkpdbqzbcoahne6.png" alt="A subsequence" width="661" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So... Now that we know about subsequence, we can try to put it into language that our partner can understand. This time, it will be JS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isSubsequence(s, t) {
  let pointerS = 0;
  let pointerT = 0;
  while (pointerS &amp;lt; s.length &amp;amp;&amp;amp; pointerT &amp;lt; t.length) {
    if (s[pointerS] == t[pointerT]) {
      pointerS++;
    }
    pointerT++;
  }
  return pointerS == s.length;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's walk through the function.&lt;br&gt;
First, our function(&lt;strong&gt;isSubsequence&lt;/strong&gt;) takes two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;s - a String which has our expected string values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;t - a String which has the letters we will check s against.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inside our function, we create two variables that we named &lt;strong&gt;pointerS&lt;/strong&gt; and &lt;strong&gt;pointerT&lt;/strong&gt;, and set their value to be 0 (that's the initial position).&lt;br&gt;
Then we enter our &lt;strong&gt;while&lt;/strong&gt; loop. Our loop will continue as long as both pointers are within the bounds of their respective strings &lt;code&gt;(pointerS &amp;lt; s.length &amp;amp;&amp;amp; pointerT &amp;lt; t.length)&lt;/code&gt;. &lt;br&gt;
Inside our loop, we check &lt;strong&gt;if&lt;/strong&gt; the current character of &lt;strong&gt;s&lt;/strong&gt; is equal to the current character of &lt;strong&gt;t&lt;/strong&gt;  ( &lt;code&gt;s[pointerS] == t[pointerT]&lt;/code&gt; ). If we find that they are equal, it means we have a match! so we can increment our &lt;strong&gt;pointerS&lt;/strong&gt;, so we will start checking another letter inside s. Now, regardless of that, we increment our &lt;strong&gt;pointerT&lt;/strong&gt;, so we move onto the next character in t.&lt;br&gt;
At the end of our while loop, if our pointer is at the end of our s string, it will mean that our s is a subsequence of t, otherwise - tough luck.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Remember - there are many ways in which you can figure it out, the important thing is that you understand what it is. I hoped I managed to bring at least a shred of clarity :) If so - it was worth it.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
