{"id":6779,"date":"2020-05-17T18:39:55","date_gmt":"2020-05-18T01:39:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6779"},"modified":"2020-05-17T18:40:39","modified_gmt":"2020-05-18T01:40:39","slug":"leetcode-1446-consecutive-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1446-consecutive-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1446. Consecutive Characters"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, the power of the string is the maximum length of a non-empty substring that&nbsp;contains only one unique character.<\/p>\n\n\n\n<p>Return&nbsp;<em>the power<\/em>&nbsp;of the string.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"leetcode\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The substring \"ee\" is of length 2 with the character 'e' only.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abbcccddddeeeeedcba\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The substring \"eeeee\" is of length 5 with the character 'e' only.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"triplepillooooow\"\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"hooraaaaaaaaaaay\"\n<strong>Output:<\/strong> 11\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"tourist\"\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 500<\/code><\/li><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Run length encoding?<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxPower(string s) {\n    char p = '?';\n    int ans = 0;\n    int cur = 0;\n    for (char c : s) {\n      if (c != p) cur = 0;      \n      p = c;\n      ans = max(ans, ++cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, the power of the string is the maximum length of a non-empty substring that&nbsp;contains only one unique character. Return&nbsp;the power&nbsp;of the string.&#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,405,4],"class_list":["post-6779","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-rle","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6779","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=6779"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6779\/revisions"}],"predecessor-version":[{"id":6781,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6779\/revisions\/6781"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}