<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nirali Patel on Medium]]></title>
        <description><![CDATA[Stories by Nirali Patel on Medium]]></description>
        <link>https://medium.com/@niralipatel047?source=rss-ff44dbccd014------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Nirali Patel on Medium</title>
            <link>https://medium.com/@niralipatel047?source=rss-ff44dbccd014------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:20:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@niralipatel047/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dynamic Programming – Longest Common Subsequence( LCS )]]></title>
            <link>https://medium.com/@niralipatel047/dynamic-programming-longest-common-subsequence-lcs-d8abeec9749f?source=rss-ff44dbccd014------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8abeec9749f</guid>
            <dc:creator><![CDATA[Nirali Patel]]></dc:creator>
            <pubDate>Thu, 05 Sep 2019 18:01:38 GMT</pubDate>
            <atom:updated>2019-09-05T18:42:29.326Z</atom:updated>
            <content:encoded><![CDATA[<h4>Objective:</h4><p>Given two string sequences, write an algorithm to find the length of longest subsequence present in both of them.</p><h4>What is Longest Common Subsequence:</h4><p>A longest subsequence is a sequence that appears in the same relative order, but not necessarily contiguous(not substring) in both the string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*5ZRA7Mtzfd1kJT-pInBOiQ.jpeg" /></figure><p>Here, We are using Dynamic Programming method to solve LCS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*g4z3guZT8_KfGvEP-GE2rg.jpeg" /></figure><h4>Dynamic Programming:</h4><p>We will solve it in Bottom-Up and store the solution of the sub problems in a solution array and use it when ever needed, This technique is called Memoization.</p><h4>Steps:</h4><ul><li>Let the input sequences be A and B of length i and j respectively.</li><li>Let the LCS(A[i],B[j]) be the length of LCS of the two sequences A and B.</li><li>If characters of both sequences match then add 1 and move to next characters on both A and B.</li><li>It they do not match then take maximum by either move characters on first sequence A or second sequence B.</li><li>Follow bottom-up approach and print LCS.</li></ul><h4>Algorithm:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*4EigJXk1QOsDUqvtXIidyw.jpeg" /></figure><h4>Print the Longest Common Subsequence:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nYSiphCE-UIb8heon_ZydA.jpeg" /></figure><p>Start from bottom right corner and track the path and mark the cell from which cell the value is coming and whenever you go diagonal ( means last character of both string has matched, so we reduce the length of both the strings by 1, so we moved diagonally), mark those cells, this is our answer.</p><h4>Application:</h4><ul><li>To finding a file</li><li>Minimum adding distance</li></ul><h4>Time-Complexity:</h4><p>If m and n are the length of the two sequences respectively then time complexity of this dynamic algorithm will be O(mn).</p><h4>Space-Complexity:</h4><p>If m and n are the size of the two sequences respectively then space required by this algorithm will be O(m+n).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8abeec9749f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Knuth-Morris-Pratt Algorithm(KMP)]]></title>
            <link>https://medium.com/@niralipatel047/the-knuth-morris-pratt-algorithm-kmp-9e8f466cfbdf?source=rss-ff44dbccd014------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e8f466cfbdf</guid>
            <dc:creator><![CDATA[Nirali Patel]]></dc:creator>
            <pubDate>Tue, 03 Sep 2019 15:00:38 GMT</pubDate>
            <atom:updated>2019-09-03T15:06:26.474Z</atom:updated>
            <content:encoded><![CDATA[<p>The key to KMP is the partial match table. The main obstacle between me and understanding KMP was the fact that I didn’t quite fully grasp what the values in the partial match table really meant. I will now try to explain them in the simplest words possible.</p><h4>The Partial Match Table</h4><p>Here’s the partial match table for the pattern “abababca”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*LKSBh16sPfMZIAnZxXsJHQ.jpeg" /></figure><p>If I have an eight-character pattern (let’s say “abababca” for the duration of this example), my partial match table will have eight cells. If I’m looking at the eighth and last cell in the table, I’m interested in the entire pattern (“abababca”). If I’m looking at the seventh cell in the table, I’m only interested in the first seven characters in the pattern (“abababc”); the eighth one (“a”) is irrelevant, and can go fall off a building or something. If I’m looking at the sixth cell of the in the table… you get the idea. Notice that I haven’t talked about what each cell means yet, but just what it’s referring to.</p><p>Now, in order to talk about the meaning, we need to know about proper prefixes and proper suffixes.</p><h4>Proper prefix:</h4><p>All the characters in a string, with one or more cut off the end. “S”, “Sn”, “Sna”, and “Snap” are all the proper prefixes of “Snape”.</p><h4>Proper suffix:</h4><p>All the characters in a string, with one or more cut off the beginning. “agrid”, “grid”, “rid”, “id”, and “d” are all proper suffixes of “Hagrid”.</p><p>With this in mind, I can now give the one-sentence meaning of the values in the partial match table:</p><h4>The length of the longest proper prefix in the (sub)pattern that matches a proper suffix in the same (sub)pattern.</h4><p>Let’s examine what I mean by that. Say we’re looking in the third cell. As you’ll remember from above, this means we’re only interested in the first three characters (“aba”). In “aba”, there are two proper prefixes (“a” and “ab”) and two proper suffixes (“a” and “ba”). The proper prefix “ab” does not match either of the two proper suffixes. However, the proper prefix “a” matches the proper suffix “a”. Thus, the length of the longest proper prefix that matches a proper suffix, in this case, is 1.</p><p>Let’s try it for cell four. Here, we’re interested in the first four characters (“abab”). We have three proper prefixes (“a”, “ab”, and “aba”) and three proper suffixes (“b”, “ab”, and “bab”). This time, “ab” is in both, and is two characters long, so cell four gets value 2.</p><p>Just because it’s an interesting example, let’s also try it for cell five, which concerns “ababa”. We have four proper prefixes (“a”, “ab”, “aba”, and “abab”) and four proper suffixes (“a”, “ba”, “aba”, and “baba”). Now, we have two matches: “a” and “aba” are both proper prefixes and proper suffixes. Since “aba” is longer than “a”, it wins, and cell five gets value 3.</p><p>Let’s skip ahead to cell seven (the second-to-last cell), which is concerned with the pattern “abababc”. Even without enumerating all the proper prefixes and suffixes, it should be obvious that there aren’t going to be any matches; all the suffixes will end with the letter “c”, and none of the prefixes will. Since there are no matches, cell seven gets 0.</p><p>Finally, let’s look at cell eight, which is concerned with the entire pattern (“abababca”). Since they both start and end with “a”, we know the value will be at least 1. However, that’s where it ends; at lengths two and up, all the suffixes contain a c, while only the last prefix (“abababc”) does. This seven-character prefix does not match the seven-character suffix (“bababca”), so cell eight gets 1.</p><h4>How to use the Partial Match Table:</h4><p>We can use the values in the partial match table to skip ahead (rather than redoing unnecessary old comparisons) when we find partial matches. The formula works like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*2ZdB3ZRHGaJVPfc0ghLydQ.jpeg" /></figure><p>Let’s say we’re matching the pattern “abababca” against the text “bacbababaabcbab”. Here’s our partial match table again for easy reference:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*cpBzFRquxgiNzxfCQBkscg.jpeg" /></figure><p>The first time we get a partial match is here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*76i-HyL3be_-ykMk_RPSZQ.jpeg" /></figure><p>This is a partial_match_length of 1. The value at table[partial_match_length - 1] (or table[0]) is 0, so we don’t get to skip ahead any. The next partial match we get is here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*X54ZGAaNp8vhoSDv-htyQQ.jpeg" /></figure><p>This is a partial_match_length of 5. The value at table[partial_match_length - 1] (or table[4]) is 3. That means we get to skip ahead partial_match_length - table[partial_match_length - 1](or 5 - table[4] or 5 - 3 or 2) characters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*yjx5t4lSY9Soh7C2l_4Emw.jpeg" /></figure><p>This is a partial_match_length of 3. The value at table[partial_match_length - 1] (or table[2]) is 1. That means we get to skip ahead partial_match_length - table[partial_match_length - 1](or 3 - table[2] or 3 - 1 or 2) characters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*C4lu4tB6yc1zNaSrYXbqcA.jpeg" /></figure><p>At this point, our pattern is longer than the remaining characters in the text, so we know there’s no match.</p><p>So, this is the explanation of finding String from the text.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e8f466cfbdf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>