[Java] 11ms, calculate on the fly with a stack to track the indices of chars
Use pointers to track the indices of chars in the characters.\nInitiate a stack of indices of length combinationLength.\n\nStarting right to left (basically sta
Use pointers to track the indices of chars in the `characters`.\nInitiate a stack of indices of length `combinationLength`.\n\nStarting right to left (basically starting from the peek element of the stack), moving the *rightmost* (the peek element) pointer to the right by 1 until it can\'t be moved further; then you would want to move the *second-right-most* pointer one step to the right (to do that, you need to pop the top element beforehand), call it `2nd_inx`, and update the *first-right-most pointer* as the `2nd_inx + 1` (i.e. push a new element to the stack as `stack.peek() + 1`). Then again, you would try to move the *first-right-most pointer* (increment the peek element), and so on!\n\n```\nclass CombinationIterator { // 11ms, beat 98%\n    private Stack<Integer> stack;\n    private int len;\n    private char[] chars;\n    public CombinationIterator(String characters, int combinationLength) {\n        len = combinationLength;\n        chars = characters.toCharArray();\n        stack = new Stack<>();\n        for (int i = 0; i < combinationLength; i++) {\n            stack.push(i);\n        }\n    }\n    \n    public String next() {\n        if (!hasNext()) return "";\n        StringBuilder sb = new StringBuilder();\n        for (int i = 0; i < len; i++) {\n            sb.append(chars[stack.get(i)]);\n        }\n        if (stack.peek() < len - 1) {\n            stack.set(stack.size() - 1, stack.peek() + 1);\n        } else {\n            while (!stack.isEmpty() && stack.peek() >= chars.length - len + stack.size() - 1) {\n                stack.pop();\n            }\n            if (!stack.isEmpty()) {\n                stack.set(stack.size() - 1, stack.peek() + 1);\n                int size = len - stack.size();\n                for (int i = 0; i < size; i++) {\n                    stack.push(stack.peek() + 1); \n                }\n            }\n         }\n        \n        return sb.toString();\n    }\n    \n    public boolean hasNext() {\n        return !stack.isEmpty();\n    }\n}\n\n```
['Stack', 'Java']