{"id":2832,"date":"2018-05-13T10:28:51","date_gmt":"2018-05-13T17:28:51","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2832"},"modified":"2018-05-13T10:29:46","modified_gmt":"2018-05-13T17:29:46","slug":"leetcode-830-positions-of-large-groups","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-830-positions-of-large-groups\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 830. Positions of Large Groups"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>In a string\u00a0<code>S<\/code>\u00a0of lowercase letters, these letters form consecutive groups of the same character.<\/p>\n<p>For example, a string like\u00a0<code>S = \"abbxxxxzyy\"<\/code>\u00a0has the groups\u00a0<code>\"a\"<\/code>,\u00a0<code>\"bb\"<\/code>,\u00a0<code>\"xxxx\"<\/code>,\u00a0<code>\"z\"<\/code>\u00a0and\u00a0<code>\"yy\"<\/code>.<\/p>\n<p>Call a group\u00a0<em>large<\/em>\u00a0if it has 3 or more characters.\u00a0 We would like the starting and ending positions of every large group.<\/p>\n<p>The final answer should be in lexicographic order.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>\"abbxxxxzzy\"\r\n<strong>Output: <\/strong>[[3,6]]\r\n<strong>Explanation<\/strong>: <code>\"xxxx\" is the single <\/code>large group with starting 3 and ending positions 6.<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>\"abc\"\r\n<strong>Output: <\/strong>[]\r\n<strong>Explanation<\/strong>: We have \"a\",\"b\" and \"c\" but no large group.\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>\"abcdddeeeeaabbbcd\"\r\n<strong>Output: <\/strong>[[3,5],[6,9],[12,14]]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:\u00a0<\/strong>\u00a0<code>1 &lt;= S.length &lt;= 1000<\/code><\/p>\n<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 18 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; largeGroupPositions(string S) {\r\n    constexpr int kLargeGroupSize = 3;\r\n    const int n = S.size();\r\n    vector&lt;vector&lt;int&gt;&gt; ans;\r\n    int c = 0;\r\n    for (int i = 0; i &lt;= n; ++i, ++c) {\r\n      if (i == n || S[i] != S[i - 1]) {\r\n        if (c &gt;= kLargeGroupSize) ans.push_back({i - c, i - 1});\r\n        c = 0;\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem In a string\u00a0S\u00a0of lowercase letters, these letters form consecutive groups of the same character. For example, a string like\u00a0S = &#8220;abbxxxxzyy&#8221;\u00a0has the groups\u00a0&#8220;a&#8221;,\u00a0&#8220;bb&#8221;,\u00a0&#8220;xxxx&#8221;,\u00a0&#8220;z&#8221;\u00a0and\u00a0&#8220;yy&#8221;. Call&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,47],"tags":[288,222,4],"class_list":["post-2832","post","type-post","status-publish","format-standard","hentry","category-array","category-string","tag-count","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2832","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=2832"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2832\/revisions"}],"predecessor-version":[{"id":2834,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2832\/revisions\/2834"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}