{"id":2867,"date":"2009-03-10T11:49:00","date_gmt":"2009-03-10T17:49:00","guid":{"rendered":"http:\/\/www.lordandrei.com\/blog\/?p=2867"},"modified":"2009-03-10T11:49:00","modified_gmt":"2009-03-10T17:49:00","slug":"algorithm-note","status":"publish","type":"post","link":"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/","title":{"rendered":"Algorithm note"},"content":{"rendered":"<p>I saw a post on one of my coding boards talking about a solution for an &#8216;interview-style&#8217; problem.<\/p>\n<p>The issue was to find the longest common substring.<\/p>\n<p>What this means is: Given the word &#8220;abracadabra&#8221; you are looking for the longest repeated set of characters.<\/p>\n<p>The longest repeated sub-string is &#8220;abra&#8221; which appears at positions 0 and 7 (counting from 0).<\/p>\n<p>The solution the person presented involved putting the strings from smallest to largest into a dictionary (hash table) with counts of iterations.<br \/>\na-4<br \/>\nb-2<br \/>\nab-2<\/p>\n<p>And then walking the tree to find the largest result. This solution seems odd to me.<\/p>\n<p>My solution is to make a dynamic array that can be queried for contents. Further, doing it from longest to shortest:<\/p>\n<p><b>Edit<\/b> <a href=\"http:\/\/gwenix.livejournal.com\/\" class=\"lj-user\">gwenix<\/a> points out a string can&#8217;t be repeated until it&#8217;s no more than half the length. So start from longest string (len \/2) rounded down.<br \/>\n<b>End Edit<\/b><\/p>\n<p>Solution:<br \/>\nGet longest string: <i>abracadabra<\/i><br \/>\nIs string in array? YES? got answer and bail; No &#8211;> add to array<br \/>\n*Reduce look string by 1 character<br \/>\nMove to start of target string:<br \/>\n**Get look string: <i>abracadabr<\/i><br \/>\nIs string in array? YES? got answer and bail; No &#8211;> add to array<br \/>\nIs look string at end of target string? Yes &#8211;> loop back to *; No &#8211;> move forward one character and loop back to **<\/p>\n<p>This is massively not optimised for time or processing. But it&#8217;s a first stab.<br \/>\nAlso, just pseudo code&#8230; loop counters would be my next pass.<\/p>\n<p>Comments? Improvements?<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2867\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2867\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-2867\" class=\"share-pinterest sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pinterest (Opens in new window)<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Tumblr (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I saw a post on one of my coding boards talking about a solution for an &#8216;interview-style&#8217; problem. The issue was to find the longest common substring. What this means is: Given the word &#8220;abracadabra&#8221; you are looking for the longest repeated set of characters. The longest repeated sub-string is &#8220;abra&#8221; which appears at positions [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2867\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2867\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-2867\" class=\"share-pinterest sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pinterest (Opens in new window)<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon no-text\" href=\"http:\/\/www.lordandrei.com\/blog\/2009\/03\/10\/algorithm-note\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Tumblr (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[1],"tags":[490,711],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1X6ba-Kf","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/posts\/2867"}],"collection":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/comments?post=2867"}],"version-history":[{"count":0,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/posts\/2867\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/media?parent=2867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/categories?post=2867"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lordandrei.com\/blog\/wp-json\/wp\/v2\/tags?post=2867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}