原题链接在这里:https://leetcode.com/problems/iterator-for-combination/
题目:
Design the CombinationIterator
class:
CombinationIterator(string characters, int combinationLength)
Initializes the object with a stringcharacters
of sorted distinct lowercase English letters and a numbercombinationLength
as arguments.next()
Returns the next combination of lengthcombinationLength
in lexicographical order.hasNext()
Returnstrue
if and only if there exists a next combination.
Example 1:
Input ["CombinationIterator", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [["abc", 2], [], [], [], [], [], []] Output [null, "ab", true, "ac", true, "bc", false] Explanation CombinationIterator itr = new CombinationIterator("abc", 2); itr.next(); // return "ab" itr.hasNext(); // return True itr.next(); // return "ac" itr.hasNext(); // return True itr.next(); // return "bc" itr.hasNext(); // return False
Constraints:
1 <= combinationLength <= characters.length <= 15
- All the characters of
characters
are unique. - At most
104
calls will be made tonext
andhasNext
. - It's guaranteed that all calls of the function
next
are valid.
题解:
It comes to how to find the next combination. Say characters = "abcd", current combination = "acd".
- Remove the common suffix with characters, which is "cd".
- Now current String = "a". Remove the right most char, which is 'a'. current String = "". Find the index of a in characters, which is 0.
- Increment that index by 1, 0++. It points to 'b'. Append this char and the following until current combination length = combinationLength.
Could use StringBuilder to maintain the current combination.
Time Complexity: CombinationIterator, O(n). next, O(n). hasNext, O(1). n = characters.length().
Space: O(n).
AC Java:
1 class CombinationIterator { 2 StringBuilder sb; 3 String s; 4 int len; 5 HashMap<Character, Integer> hm; 6 7 public CombinationIterator(String characters, int combinationLength) { 8 this.sb = new StringBuilder(); 9 this.s = characters; 10 this.len = combinationLength; 11 this.hm = new HashMap<>(); 12 13 for(int i = 0; i<s.length(); i++){ 14 char c = s.charAt(i); 15 if(sb.length() < len){ 16 sb.append(c); 17 } 18 19 hm.put(c, i); 20 } 21 } 22 23 public String next() { 24 String res = sb.toString(); 25 int ind = s.length() - 1; 26 while(ind >= 0 && sb.length() != 0 && sb.charAt(sb.length() -1) == s.charAt(ind)){ 27 sb.deleteCharAt(sb.length() - 1); 28 ind--; 29 } 30 31 if(sb.length() == 0){ 32 return res; 33 } 34 35 char c = sb.charAt(sb.length() - 1); 36 ind = hm.get(c) + 1; 37 sb.deleteCharAt(sb.length() - 1); 38 39 while(sb.length() < len && ind < s.length()){ 40 sb.append(s.charAt(ind)); 41 ind++; 42 } 43 44 return res; 45 } 46 47 public boolean hasNext() { 48 return sb.length() != 0; 49 } 50 } 51 52 /** 53 * Your CombinationIterator object will be instantiated and called as such: 54 * CombinationIterator obj = new CombinationIterator(characters, combinationLength); 55 * String param_1 = obj.next(); 56 * boolean param_2 = obj.hasNext(); 57 */