File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } characters
3+ * @param {number } combinationLength
4+ */
5+ let CombinationIterator = function ( characters , combinationLength ) {
6+ let cl = characters . length
7+ let res = [ ]
8+ let helper = ( start , prev ) => {
9+ let pl = prev . length
10+ for ( let i = start ; i < cl ; i ++ ) {
11+ let rest = cl - i
12+ let diff = combinationLength - pl
13+ if ( diff > rest ) {
14+ return
15+ }
16+ let next = prev + characters [ i ]
17+ if ( next . length == combinationLength ) {
18+ res . push ( next )
19+ } else {
20+ helper ( i + 1 , next )
21+ }
22+ }
23+ }
24+ helper ( 0 , "" )
25+ this . res = res . sort ( ( a , b ) => ( a > b ? 1 : - 1 ) )
26+ this . point = 0
27+ }
28+
29+ /**
30+ * @return {string }
31+ */
32+ CombinationIterator . prototype . next = function ( ) {
33+ if ( ! this . hasNext ( ) ) {
34+ return undefined
35+ }
36+ let val = this . res [ this . point ++ ]
37+ return val
38+ }
39+
40+ /**
41+ * @return {boolean }
42+ */
43+ CombinationIterator . prototype . hasNext = function ( ) {
44+ return this . point !== this . res . length
45+ }
46+
47+ /**
48+ * Your CombinationIterator object will be instantiated and called as such:
49+ * var obj = new CombinationIterator(characters, combinationLength)
50+ * var param_1 = obj.next()
51+ * var param_2 = obj.hasNext()
52+ */
You can’t perform that action at this time.
0 commit comments