public String reverse(String str) {

        char charsOfStr[] = str.toCharArray();
        Stack<Character> stack = new Stack<>();

        // first iteration to populate stack
        for(char ch : charsOfStr) {
            if(ch >='0' && ch<='9') {
                if(ch % 3 == 0) {
                    stack.push(ch);
                }
            }
        }

        // reverse multiples of 3
        for(int i=0; i<charsOfStr.length; i++) {
            if(charsOfStr[i] >='0' && charsOfStr[i]<='9') {
                if(charsOfStr[i] % 3 == 0) {
                    charsOfStr[i] = stack.pop();
                }
            }
        }

        return new String(charsOfStr);
    }