{"id":26963,"date":"2018-01-05T21:01:32","date_gmt":"2018-01-05T15:31:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26963"},"modified":"2018-01-05T21:01:32","modified_gmt":"2018-01-05T15:31:32","slug":"c-programming-given-two-strings-find-first-string-sub-sequence-second","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-given-two-strings-find-first-string-sub-sequence-second\/","title":{"rendered":"C Programming &#8211; Given two strings, find if first string is a subsequence of second"},"content":{"rendered":"<p>Given two strings str1 and str2, find if str1 is a subsequence of str2. A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements (source: wiki). Expected time complexity is linear.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<p>Input: str1 = &#8220;AXY&#8221;, str2 = &#8220;ADXCPY&#8221;<br \/>\nOutput: True (str1 is a subsequence of str2)<\/p>\n<p>Input: str1 = &#8220;AXY&#8221;, str2 = &#8220;YADXCP&#8221;<br \/>\nOutput: False (str1 is not a subsequence of str2)<\/p>\n<p>Input: str1 = &#8220;gksrek&#8221;, str2 = &#8220;geeksforgeeks&#8221;<br \/>\nOutput: True (str1 is a subsequence of str2)<\/p>\n<p>The idea is simple, we traverse both strings from one side to other side (say from rightmost character to leftmost). If we find a matching character, we move ahead in both strings. Otherwise we move ahead only in str2.<\/p>\n<p>Following is <strong>Recursive Implementation<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Recursive C++ program to check if a string is subsequence of another string<br\/>#include&lt;iostream&gt;<br\/>#include&lt;cstring&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Returns true if str1[] is a subsequence of str2[]. m is<br\/>\/\/ length of str1 and n is length of str2<br\/>bool isSubSequence(char str1[], char str2[], int m, int n)<br\/>{<br\/>    \/\/ Base Cases<br\/>    if (m == 0) return true;<br\/>    if (n == 0) return false;<br\/> <br\/>    \/\/ If last characters of two strings are matching<br\/>    if (str1[m-1] == str2[n-1])<br\/>        return isSubSequence(str1, str2, m-1, n-1);<br\/> <br\/>    \/\/ If last characters are not matching<br\/>    return isSubSequence(str1, str2, m, n-1);<br\/>}<br\/> <br\/>\/\/ Driver program to test methods of graph class<br\/>int main()<br\/>{<br\/>    char str1[] = &quot;gksrek&quot;;<br\/>    char str2[] = &quot;geeksforgeeks&quot;;<br\/>    int m = strlen(str1);<br\/>    int n = strlen(str2);<br\/>    isSubSequence(str1, str2, m, n)? cout &lt;&lt; &quot;Yes &quot;:<br\/>                                     cout &lt;&lt; &quot;No&quot;;<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Yes<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Given two strings str1 and str2, find if str1 is a subsequence of str2. A subsequence is a sequence that can be derived from another sequence by deleting<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1],"tags":[75061,73170,73158,73394,76934,71496,76933,71145,78226,72834,74751,76930,76935,76929,75059,80456,76924,76938,75689,76923,73148,76928],"class_list":["post-26963","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","tag-c-programming-character","tag-c-programs-on-strings","tag-c-string-functions","tag-compare-strings-in-c","tag-copy-string-in-c","tag-dynamic-programming-algorithm","tag-how-to-input-a-string-in-c","tag-quicksort-c","tag-sequence-definition","tag-string-array-in-c","tag-string-compare-in-c","tag-string-functions-in-c","tag-string-functions-in-c-with-examples","tag-string-h-functions","tag-string-in-c-language","tag-string-length-in-c","tag-string-manipulation-functions-in-c","tag-string-manipulation-in-c","tag-string-operations-in-c","tag-string-programming","tag-string-programs-in-c","tag-write-a-program-to-accept-string-from-user"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26963"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26963\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}