{"id":2053,"date":"2018-03-10T22:45:04","date_gmt":"2018-03-11T06:45:04","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2053"},"modified":"2018-03-15T21:48:07","modified_gmt":"2018-03-16T04:48:07","slug":"leetcode-796-rotate-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-796-rotate-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 796. Rotate String"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32A, B, \u95ee\u80fd\u5426\u901a\u8fc7\u65cb\u8f6cA\u5f97\u5230B\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/rotate-string\/description\/\">https:\/\/leetcode.com\/problems\/rotate-string\/description\/<\/a><\/p>\n<p>We are given two strings,\u00a0<code>A<\/code>\u00a0and\u00a0<code>B<\/code>.<\/p>\n<p>A\u00a0<em>shift on\u00a0<code>A<\/code><\/em>\u00a0consists of taking string\u00a0<code>A<\/code>\u00a0and moving the leftmost character to the rightmost position. For example, if\u00a0<code>A = 'abcde'<\/code>, then it will be\u00a0<code>'bcdea'<\/code>\u00a0after one shift on\u00a0<code>A<\/code>. Return\u00a0<code>True<\/code>\u00a0if and only if\u00a0<code>A<\/code>\u00a0can become\u00a0<code>B<\/code>\u00a0after some number of shifts on\u00a0<code>A<\/code>.<\/p>\n<pre class=\"\">Example 1:\r\nInput: A = 'abcde', B = 'cdeab'\r\nOutput: true\r\n\r\nExample 2:\r\nInput: A = 'abcde', B = 'abced'\r\nOutput: false\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>A<\/code>\u00a0and\u00a0<code>B<\/code>\u00a0will have length at most\u00a0<code>100<\/code>.<\/li>\n<\/ul>\n<p><strong>Solution 1: Brute Force<\/strong><\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n  bool rotateString(string A, string B) {\r\n    if (A.length() != B.length()) return false;\r\n    for (int i = 1; i &lt; A.length(); ++i)\r\n      if (check(A, B, i)) return true;\r\n    return false;\r\n  }\r\nprivate:\r\n  bool check(const string&amp; A, const string&amp; B, int offset) {\r\n    for (int i = 0; i &lt; A.length(); ++i)\r\n      if (A[(i + offset) % A.length()] != B[i])\r\n        return false;\r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32A, B, \u95ee\u80fd\u5426\u901a\u8fc7\u65cb\u8f6cA\u5f97\u5230B\u3002 Problem: https:\/\/leetcode.com\/problems\/rotate-string\/description\/ We are given two strings,\u00a0A\u00a0and\u00a0B. A\u00a0shift on\u00a0A\u00a0consists of taking string\u00a0A\u00a0and moving the leftmost character to the rightmost position. For example,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,250,4],"class_list":["post-2053","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-rotate","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2053"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2053\/revisions"}],"predecessor-version":[{"id":2056,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2053\/revisions\/2056"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}