CombinationIterator(string characters, int combinationLength) Initializes the object with a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
next() Returns the next combination of length combinationLength in lexicographical order.
hasNext() Returns true if and only if there exists a next combination.
classCombinationIterator:def__init__(self,characters:str,combinationLength:int):defdfs(i):iflen(t)==combinationLength:cs.append(''.join(t))returnifi==n:returnt.append(characters[i])dfs(i+1)t.pop()dfs(i+1)cs=[]n=len(characters)t=[]dfs(0)self.cs=csself.idx=0defnext(self)->str:ans=self.cs[self.idx]self.idx+=1returnansdefhasNext(self)->bool:returnself.idx<len(self.cs)# Your CombinationIterator object will be instantiated and called as such:# obj = CombinationIterator(characters, combinationLength)# param_1 = obj.next()# param_2 = obj.hasNext()
classCombinationIterator{privateintn;privateintcombinationLength;privateStringcharacters;privateStringBuildert=newStringBuilder();privateList<String>cs=newArrayList<>();privateintidx=0;publicCombinationIterator(Stringcharacters,intcombinationLength){n=characters.length();this.combinationLength=combinationLength;this.characters=characters;dfs(0);}publicStringnext(){returncs.get(idx++);}publicbooleanhasNext(){returnidx<cs.size();}privatevoiddfs(inti){if(t.length()==combinationLength){cs.add(t.toString());return;}if(i==n){return;}t.append(characters.charAt(i));dfs(i+1);t.deleteCharAt(t.length()-1);dfs(i+1);}}/** * Your CombinationIterator object will be instantiated and called as such: * CombinationIterator obj = new CombinationIterator(characters, combinationLength); * String param_1 = obj.next(); * boolean param_2 = obj.hasNext(); */
classCombinationIterator{public:stringcharacters;vector<string>cs;intidx;intn;intcombinationLength;stringt;CombinationIterator(stringcharacters,intcombinationLength){idx=0;n=characters.size();this->characters=characters;this->combinationLength=combinationLength;dfs(0);}stringnext(){returncs[idx++];}boolhasNext(){returnidx<cs.size();}voiddfs(inti){if(t.size()==combinationLength){cs.push_back(t);return;}if(i==n)return;t.push_back(characters[i]);dfs(i+1);t.pop_back();dfs(i+1);}};/** * Your CombinationIterator object will be instantiated and called as such: * CombinationIterator* obj = new CombinationIterator(characters, combinationLength); * string param_1 = obj->next(); * bool param_2 = obj->hasNext(); */
typeCombinationIteratorstruct{cs[]stringidxint}funcConstructor(charactersstring,combinationLengthint)CombinationIterator{t:=[]byte{}n:=len(characters)cs:=[]string{}vardfsfunc(int)dfs=func(iint){iflen(t)==combinationLength{cs=append(cs,string(t))return}ifi==n{return}t=append(t,characters[i])dfs(i+1)t=t[:len(t)-1]dfs(i+1)}dfs(0)returnCombinationIterator{cs,0}}func(this*CombinationIterator)Next()string{ans:=this.cs[this.idx]this.idx++returnans}func(this*CombinationIterator)HasNext()bool{returnthis.idx<len(this.cs)}/** * Your CombinationIterator object will be instantiated and called as such: * obj := Constructor(characters, combinationLength); * param_1 := obj.Next(); * param_2 := obj.HasNext(); */
classCombinationIterator:def__init__(self,characters:str,combinationLength:int):self.curr=(1<<len(characters))-1self.size=combinationLengthself.cs=characters[::-1]defnext(self)->str:whileself.curr>=0andself.curr.bit_count()!=self.size:self.curr-=1ans=[]foriinrange(len(self.cs)):if(self.curr>>i)&1:ans.append(self.cs[i])self.curr-=1return''.join(ans[::-1])defhasNext(self)->bool:whileself.curr>=0andself.curr.bit_count()!=self.size:self.curr-=1returnself.curr>=0# Your CombinationIterator object will be instantiated and called as such:# obj = CombinationIterator(characters, combinationLength)# param_1 = obj.next()# param_2 = obj.hasNext()
classCombinationIterator{privateintcurr;privateintsize;privatechar[]cs;publicCombinationIterator(Stringcharacters,intcombinationLength){intn=characters.length();curr=(1<<n)-1;size=combinationLength;cs=newchar[n];for(inti=0;i<n;++i){cs[i]=characters.charAt(n-i-1);}}publicStringnext(){while(curr>=0&&Integer.bitCount(curr)!=size){--curr;}StringBuilderans=newStringBuilder();for(inti=0;i<cs.length;++i){if(((curr>>i)&1)==1){ans.append(cs[i]);}}--curr;returnans.reverse().toString();}publicbooleanhasNext(){while(curr>=0&&Integer.bitCount(curr)!=size){--curr;}returncurr>=0;}}/** * Your CombinationIterator object will be instantiated and called as such: * CombinationIterator obj = new CombinationIterator(characters, combinationLength); * String param_1 = obj.next(); * boolean param_2 = obj.hasNext(); */
classCombinationIterator{public:intsize;stringcs;intcurr;CombinationIterator(stringcharacters,intcombinationLength){intn=characters.size();curr=(1<<n)-1;reverse(characters.begin(),characters.end());cs=characters;size=combinationLength;}stringnext(){while(curr>=0&&__builtin_popcount(curr)!=size)--curr;stringans;for(inti=0;i<cs.size();++i){if((curr>>i)&1){ans+=cs[i];}}reverse(ans.begin(),ans.end());--curr;returnans;}boolhasNext(){while(curr>=0&&__builtin_popcount(curr)!=size)--curr;returncurr>=0;}};/** * Your CombinationIterator object will be instantiated and called as such: * CombinationIterator* obj = new CombinationIterator(characters, combinationLength); * string param_1 = obj->next(); * bool param_2 = obj->hasNext(); */
typeCombinationIteratorstruct{currintsizeintcs[]byte}funcConstructor(charactersstring,combinationLengthint)CombinationIterator{n:=len(characters)curr:=(1<<n)-1size:=combinationLengthcs:=make([]byte,n)fori:=rangecharacters{cs[n-i-1]=characters[i]}returnCombinationIterator{curr,size,cs}}func(this*CombinationIterator)Next()string{forthis.curr>=0&&bits.OnesCount(uint(this.curr))!=this.size{this.curr--}ans:=[]byte{}fori:=rangethis.cs{if(this.curr>>i&1)==1{ans=append(ans,this.cs[i])}}fori,j:=0,len(ans)-1;i<j;i,j=i+1,j-1{ans[i],ans[j]=ans[j],ans[i]}this.curr--returnstring(ans)}func(this*CombinationIterator)HasNext()bool{forthis.curr>=0&&bits.OnesCount(uint(this.curr))!=this.size{this.curr--}returnthis.curr>=0}/** * Your CombinationIterator object will be instantiated and called as such: * obj := Constructor(characters, combinationLength); * param_1 := obj.Next(); * param_2 := obj.HasNext(); */