{"id":33111,"date":"2023-02-04T13:06:20","date_gmt":"2023-02-04T07:36:20","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/"},"modified":"2023-02-04T13:06:20","modified_gmt":"2023-02-04T07:36:20","slug":"solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/","title":{"rendered":"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-38162352\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"38162352\" data-parentid=\"38161971\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>This question can be simply solved by using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/e-maxx-eng.github.io\/string\/z-function.html\">z-function<\/a> in linear time.<\/p>\n<pre><code>int NumberOfcopies(String t, String h){\n    \/\/ t = the first string i.e \"ooo\"\n    \/\/ h = the second string i.e \"wooooooooooooooooooooow\"\n\n    String s = t + \"$\" + h; \/\/ adding a sentinel character in between\n    int n = s.length(); \/\/ length of new string\n    int[] z = new int[n]; \/\/ z array\n\n    \/\/ Code ref : http:\/\/e-maxx-eng.github.io\/string\/z-function.html\n    int l = 0, r = 0;\n    for (int i = 1; i &lt; n; i++){\n        if (i &lt;= r)\n            z[i] = Math.min(r - i + 1, z[i - 1]);\n        while (i + z[i] &lt; n &amp;&amp; s.charAt(z[i]) == s.charAt(i + z[i]))\n            ++z[i];\n        if (i + z[i] - 1 &gt; r){\n            l = i;\n            r = i + z[i] - 1;\n        }\n    }\n\n    \/\/checking for all the occurance of string t in string h\n    int res = 0;\n    for (int i = t.length() + 1; i &lt; n; ){\n        if (z[i] == t.length()){\n            \/\/A copy is found so skip next t.length chars\n            i += t.length();\n            ++res; \n        }\n        else ++i;\n    }\n    System.out.println(\"Number of Occurance : \" + res);\n    return res;\n}\n<\/code><\/pre>\n<blockquote>\n<p>The Z-function for this string is an array of length <b>n<\/b> where<br \/>\n  the <b>i-th<\/b> element is equal to the greatest number of characters<br \/>\n  starting from the position <b>i<\/b> that coincide with the first<br \/>\n  characters of s.<\/p>\n<\/blockquote>\n<p>This can be exploited to find the number of occurrences of a string <b>t<\/b> in another string <b>h<\/b>. Suppose we join the string t and h with a sentinel character in between (Sentinel character is a character that does not occur in either of the strings) to form a new string <b>s<\/b>. <\/p>\n<p>Lets us calculate the z function in array <code>z[]<\/code>.<\/p>\n<p>Now lets start searching from the character after the sentinel character i.e. the characters of string h. For i-th character in string h ( such that i-th character belongs to string s) if <code>z[i]<\/code> equals to length of string t (the pattern) then it implies that starting from i-th char, the <code>t.length()<\/code> chars are same as the first <code>t.length()<\/code> chars of string s which is what the string t equals.<\/p>\n<p>example : <br \/>\nt = <i>ab <br \/>\nh = aaabaab<\/i> <\/p>\n<p>s = a b $ a a a b a a b <br \/>\nz = 0 0 0 1 1 2 0 1 2 0 <br \/>\ni = 0 1 2 3 4 5 6 7 8 9 <\/p>\n<p>for <code>i = 5<\/code> we can see that <code>z[i] == t.length()<\/code>, that means we found a copy. Now to prevent Overlapping solutions, we skip the <code>t.length()<\/code> chars hence now <code>i = 7<\/code><\/p>\n<p>continuing this will get you the result.   <\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to write a program that returns number of occurrences of a string in another string? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] This question can be simply solved by using z-function in linear time. int NumberOfcopies(String t, String h){ \/\/ t = the first string i.e &#8220;ooo&#8221; \/\/ h = the second string i.e &#8220;wooooooooooooooooooooow&#8221; String s = t + &#8220;$&#8221; + h; \/\/ adding a sentinel character in between int n = s.length(); \/\/ length &#8230; <a title=\"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/\" aria-label=\"More on [Solved] How to write a program that returns number of occurrences of a string in another string? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323,362],"class_list":["post-33111","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How to write a program that returns number of occurrences of a string in another string? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] This question can be simply solved by using z-function in linear time. int NumberOfcopies(String t, String h){ \/\/ t = the first string i.e &quot;ooo&quot; \/\/ h = the second string i.e &quot;wooooooooooooooooooooow&quot; String s = t + &quot;$&quot; + h; \/\/ adding a sentinel character in between int n = s.length(); \/\/ length ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-04T07:36:20+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed]\",\"datePublished\":\"2023-02-04T07:36:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/\"},\"wordCount\":259,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"java\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/\",\"name\":\"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2023-02-04T07:36:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed] - JassWeb","og_description":"[ad_1] This question can be simply solved by using z-function in linear time. int NumberOfcopies(String t, String h){ \/\/ t = the first string i.e \"ooo\" \/\/ h = the second string i.e \"wooooooooooooooooooooow\" String s = t + \"$\" + h; \/\/ adding a sentinel character in between int n = s.length(); \/\/ length ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/","og_site_name":"JassWeb","article_published_time":"2023-02-04T07:36:20+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed]","datePublished":"2023-02-04T07:36:20+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/"},"wordCount":259,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/","name":"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-04T07:36:20+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-write-a-program-that-returns-number-of-occurrences-of-a-string-in-another-string-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to write a program that returns number of occurrences of a string in another string? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=33111"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33111\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}