{"id":7748,"date":"2020-11-30T00:09:26","date_gmt":"2020-11-30T08:09:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7748"},"modified":"2020-11-30T00:17:59","modified_gmt":"2020-11-30T08:17:59","slug":"leetcode-1286-iterator-for-combination","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1286-iterator-for-combination\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1286. Iterator for Combination"},"content":{"rendered":"\n<p>Design an Iterator class, which has:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A constructor that takes a string&nbsp;<code>characters<\/code>&nbsp;of&nbsp;<strong>sorted distinct<\/strong>&nbsp;lowercase English letters and a number&nbsp;<code>combinationLength<\/code>&nbsp;as arguments.<\/li><li>A function&nbsp;<em>next()<\/em>&nbsp;that returns the next combination of length&nbsp;<code>combinationLength<\/code>&nbsp;in&nbsp;<strong>lexicographical order<\/strong>.<\/li><li>A function&nbsp;<em>hasNext()<\/em>&nbsp;that returns&nbsp;<code>True<\/code>&nbsp;if and only if&nbsp;there exists a next combination.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">CombinationIterator iterator = new CombinationIterator(\"abc\", 2); \/\/ creates the iterator.\n\niterator.next(); \/\/ returns \"ab\"\niterator.hasNext(); \/\/ returns true\niterator.next(); \/\/ returns \"ac\"\niterator.hasNext(); \/\/ returns true\niterator.next(); \/\/ returns \"bc\"\niterator.hasNext(); \/\/ returns false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= combinationLength &lt;=&nbsp;characters.length &lt;= 15<\/code><\/li><li>There will be at most&nbsp;<code>10^4<\/code>&nbsp;function calls per test.<\/li><li>It&#8217;s guaranteed that all&nbsp;calls&nbsp;of the function&nbsp;<code>next<\/code>&nbsp;are valid.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bitmask<\/strong><\/h2>\n\n\n\n<p>Use a bitmask to represent the chars selected.<br>start with (2^n &#8211; 1), decrease the mask until there are c bit set.<br>stop when mask reach to 0.<\/p>\n\n\n\n<p>mask: 111 =&gt; abc<br>mask: 110 =&gt; ab<br>mask: 101 =&gt; ac<br>mask: 011 =&gt; bc<br>mask: 000 =&gt; &#8220;&#8221; Done<\/p>\n\n\n\n<p>Time complexity: O(2^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++\">\nclass CombinationIterator {\npublic:\n  CombinationIterator(string characters, int combinationLength): \n    chars_(rbegin(characters), rend(characters)),\n    length_(combinationLength),\n    mask_((1 << characters.size()) - 1) {}\n\n  string next() {\n    hasNext();    \n    string ans;\n    for (int i = chars_.size() - 1; i >= 0 ; --i)\n      if ((mask_ >> i) & 1)\n        ans.push_back(chars_[i]);\n    --mask_;\n    return ans;\n  }\n\n  bool hasNext() {\n    while (mask_ >= 0 && __builtin_popcount(mask_) != length_) --mask_;\n    return mask_ > 0;\n  }\nprivate:\n  int mask_ = 0;  \n  const int length_;\n  const string chars_;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design an Iterator class, which has: A constructor that takes a string&nbsp;characters&nbsp;of&nbsp;sorted distinct&nbsp;lowercase English letters and a number&nbsp;combinationLength&nbsp;as arguments. A function&nbsp;next()&nbsp;that returns the next combination&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[122,33,177,4],"class_list":["post-7748","post","type-post","status-publish","format-standard","hentry","category-bit","tag-combination","tag-dfs","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7748","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=7748"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7748\/revisions"}],"predecessor-version":[{"id":7750,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7748\/revisions\/7750"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}