{"id":443,"date":"2013-05-13T16:18:50","date_gmt":"2013-05-13T23:18:50","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=443"},"modified":"2013-05-13T16:20:41","modified_gmt":"2013-05-13T23:20:41","slug":"ctci-ch-1-8","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=443","title":{"rendered":"CTCI: Ch. 1-8"},"content":{"rendered":"<p>\n\t<strong>Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., &ldquo;waterbottle&rdquo; is a rotation of &ldquo;erbottlewat&rdquo;).<\/strong>\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<pre class=\"brush:cpp;\">\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nbool isSubstring(string s1, string s2)\r\n{\r\n    \/*\r\n        string::find\r\n        The position of the first character of the first match.\r\n        If no matches were found, the function returns string::npos.\r\n     *\/\r\n    if(s1.find(s2) != s1.npos)\r\n        return true;\r\n    else\r\n        return false;\r\n}\r\nbool isRotation(string s1, string s2)\r\n{\r\n    if( (s1.length() == s2.length()) &amp;&amp; s1.length() &gt; 0)\r\n        return isSubstring(s1+s1, s2);\r\n    else\r\n        return false;\r\n}\r\n\r\nint main(int argc, const char * argv[])\r\n{\r\n    string s1 = &quot;abcdef&quot;;\r\n    string s2 = &quot;cdefab&quot;;\r\n    if(isRotation(s1,s2))\r\n        cout&lt;&lt;&quot;TRUE&quot;&lt;&lt;endl;\r\n    else\r\n        cout&lt;&lt;&quot;FALSE&quot;&lt;&lt;endl;\r\n    \r\n    return 0;\r\n}\r\n<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., &hellip; <a href=\"http:\/\/cywang.no-ip.org\/wordpress\/?p=443\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"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,"footnotes":""},"categories":[19,6],"tags":[],"class_list":["post-443","post","type-post","status-publish","format-standard","hentry","category-chapter-1-arrays-and-strings","category-cracking-the-coding-interview"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=443"}],"version-history":[{"count":2,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":445,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/443\/revisions\/445"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}