{"id":110,"date":"2017-08-01T20:11:09","date_gmt":"2017-08-01T12:11:09","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=110"},"modified":"2018-11-27T16:16:27","modified_gmt":"2018-11-27T08:16:27","slug":"38-count-and-say","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=110","title":{"rendered":"38. Count and Say"},"content":{"rendered":"<p>The count-and-say sequence is the sequence of integers with the first five terms as following:<\/p>\n<pre>1.     1\r\n2.     11\r\n3.     21\r\n4.     1211\r\n5.     111221\r\n<\/pre>\n<p><code>1<\/code>\u00a0is read off as\u00a0<code>\"one 1\"<\/code>\u00a0or\u00a0<code>11<\/code>.<br \/>\n<code>11<\/code>\u00a0is read off as\u00a0<code>\"two 1s\"<\/code>\u00a0or\u00a0<code>21<\/code>.<br \/>\n<code>21<\/code>\u00a0is read off as\u00a0<code>\"one 2<\/code>, then\u00a0<code>one 1\"<\/code>\u00a0or\u00a0<code>1211<\/code>.<\/p>\n<p>Given an integer\u00a0<i>n<\/i>\u00a0where 1 \u2264\u00a0<em>n<\/em>\u00a0\u2264 30, generate the\u00a0<i>n<\/i><sup>th<\/sup>\u00a0term of the count-and-say sequence.<\/p>\n<p>Note: Each term of the sequence of integers will be represented as a string.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre><b>Input:<\/b> 1\r\n<b>Output:<\/b> \"1\"\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre><b>Input:<\/b> 4\r\n<b>Output:<\/b> \"1211\"<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def countAndSay(self, n):\r\n        &quot;&quot;&quot;\r\n        :type n: int\r\n        :rtype: str\r\n        1.     1\r\n        2.     11\r\n        3.     21\r\n        4.     1211\r\n        5.     111221\r\n        6.     312211\r\n        7.     13112221\r\n\r\n        &quot;&quot;&quot;\r\n        d = {1: '1', 2: '11', 3: '21', 4: '1211', 5: '111221', 6: '312211', 7: '13112221', 8: '1113213211', 9: '31131211131221',\r\n             10: '13211311123113112211', 11: '11131221133112132113212221', 12: '3113112221232112111312211312113211',\r\n             13: '1321132132111213122112311311222113111221131221',\r\n             14: '11131221131211131231121113112221121321132132211331222113112211',\r\n             15: '311311222113111231131112132112311321322112111312211312111322212311322113212221',\r\n             16: '132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211',\r\n             17: '11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221',\r\n             18: '31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211',\r\n             19: '1321132132211331121321231231121113112221121321132122311211131122211211131221131211132221121321132132212321121113121112133221123113112221131112311332111213122112311311123112111331121113122112132113213211121332212311322113212221',\r\n             20: '11131221131211132221232112111312111213111213211231132132211211131221131211221321123113213221123113112221131112311332211211131221131211132211121312211231131112311211232221121321132132211331121321231231121113112221121321133112132112312321123113112221121113122113121113123112112322111213211322211312113211'}\r\n\r\n        if n in d:\r\n            print 1111\r\n            return d[n]\r\n        result = d[20]\r\n        for i in range(19, n-1):\r\n            result = self.get_new_result(result)\r\n        return result\r\n\r\n    def get_new_result(self, last_resule):\r\n        result = &quot;&quot;\r\n        num = 0\r\n        temp_result = &quot;a&quot;\r\n        for i in last_resule:\r\n            if i == temp_result:\r\n                num += 1\r\n            else:\r\n                if temp_result == &quot;a&quot;:\r\n                    temp_result = i\r\n                    num = 1\r\n                else:\r\n                    result += str(num) + temp_result\r\n                    temp_result = i\r\n                    num = 1\r\n        result += str(num) + temp_result\r\n        return result\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The count-and-say sequence is the sequence of integers <a href=\"https:\/\/www.ccagml.com\/?p=110\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">38. Count and Say<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/110"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}