1286. Iterator for Combination Medium

@problem@discussion
#String#Backtracking#Design#Iterator



1/**
2 * [1286] Iterator for Combination
3 *
4 * Design the CombinationIterator class:
5 * 
6 * 	CombinationIterator(string characters, int combinationLength) Initializes the object with a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.
7 * 	next() Returns the next combination of length combinationLength in lexicographical order.
8 * 	hasNext() Returns true if and only if there exists a next combination.
9 * 
10 *  
11 * Example 1:
12 * 
13 * Input
14 * ["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
15 * [["abc", 2], [], [], [], [], [], []]
16 * Output
17 * [null, "ab", true, "ac", true, "bc", false]
18 * Explanation
19 * CombinationIterator itr = new CombinationIterator("abc", 2);
20 * itr.next();    // return "ab"
21 * itr.hasNext(); // return True
22 * itr.next();    // return "ac"
23 * itr.hasNext(); // return True
24 * itr.next();    // return "bc"
25 * itr.hasNext(); // return False
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= combinationLength <= characters.length <= 15
31 * 	All the characters of characters are unique.
32 * 	At most 10^4 calls will be made to next and hasNext.
33 * 	It is guaranteed that all calls of the function next are valid.
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/iterator-for-combination/
39// discuss: https://leetcode.com/problems/iterator-for-combination/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43struct CombinationIterator {
44        vec![]
45    }
46
47
48/** 
49 * `&self` means the method takes an immutable reference.
50 * If you need a mutable reference, change it to `&mut self` instead.
51 */
52impl CombinationIterator {
53
54    fn new(characters: String, combinationLength: i32) -> Self {
55        
56    }
57    
58    fn next(&self) -> String {
59        
60    }
61    
62    fn has_next(&self) -> bool {
63        
64    }
65}
66
67/**
68 * Your CombinationIterator object will be instantiated and called as such:
69 * let obj = CombinationIterator::new(characters, combinationLength);
70 * let ret_1: String = obj.next();
71 * let ret_2: bool = obj.has_next();
72 */
73
74// submission codes end
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_1286() {
82    }
83}
84


Back
© 2025 bowen.ge All Rights Reserved.