{"id":4588,"date":"2016-06-22T02:14:47","date_gmt":"2016-06-22T02:14:47","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4588"},"modified":"2016-06-22T02:14:47","modified_gmt":"2016-06-22T02:14:47","slug":"javascript-function-find-overlap-two-strings","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/javascript-function-find-overlap-two-strings\/","title":{"rendered":"Javascript function to find the overlap between two strings"},"content":{"rendered":"<p>Finding where two strings overlap is an interesting programming problem, easily solved recursively. <\/p>\n<p>This is a common problem in some parts of computer science, for instance DNA processing, which takes many small strands of DNA and re-combines them by finding the bits that overlap. It also happens when you&#8217;re using subtitles &#8211; these can repeat in a subtitle file, especially in the newer formats like WebVTT where you can specify highlighted texts in each time window (highlighting the word being spoken).<\/p>\n<pre lang=\"javascript\">\nfunction findOverlap(a, b) {\n  if (b.length === 0) {\n    return \"\";\n  }\n\n  if (a.endsWith(b)) {\n    return b;\n  }\n\n  if (a.indexOf(b) >= 0) {\n    return b;\n  }\n\n  return findOverlap(a, b.substring(0, b.length - 1));\n}\n<\/pre>\n<p>Some test cases:<\/p>\n<pre lang=\"javascript\">\nfindOverlap(\"12345\", \"aaa\")\n\"\"\nfindOverlap(\"12345\", \"12\")\n\"12\"\nfindOverlap(\"12345\", \"345\")\n\"345\"\nfindOverlap(\"12345\", \"3456\")\n\"345\"\nfindOverlap(\"12345\", \"111\")\n\"1\"\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Finding the overlapping bits of two strings with javascript<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4588"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4588"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4588\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}