Simple Java Solution with Comments
```\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        TreeMap tm = new TreeMap<>(Collections.reverseOrder());\n    
```\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        TreeMap<Character, Integer> tm = new TreeMap<>(Collections.reverseOrder());\n        StringBuffer sb = new StringBuffer();\n        boolean shouldGetLargest = true;\n        for(char c: s.toCharArray())\n        {\n            tm.put(c, tm.getOrDefault(c,0)+1);      \n        }\n        while(tm.size()>0){\n            char c = tm.firstKey();// get the largest character\n            if(sb.length()!=0 && sb.charAt(sb.length()-1) == c){ // if last char of sb is same as largest character\n               shouldGetLargest = false;     \n            }\n            int charCount =0, count = 0;\n\n            if(shouldGetLargest){\n                charCount = tm.get(c);\n                count = Math.min(charCount,repeatLimit);\n            }else{\n                //get second largest\n                if(tm.size()>1){\n                char largest = c;\n                c = tm.subMap(largest, false, \'a\', true).firstKey(); // exclude largest\n                charCount = tm.get(c);\n                count = 1;// use second largest char only once to build lexicographically largest\n                }\n                // if should get next largest and only one key exists we should drop them - happens automatically because count == charCount\n            }\n                if(count==charCount)\n                    tm.remove(c);\n                else\n                tm.put(c, charCount-count);\n\n                while(count>0){ //insert till repeat limit or available character count\n                    sb.append(c);\n                    count--;\n                }\n            \n            if(!shouldGetLargest)// if we used second largest this time, use largest next time\n                shouldGetLargest = !shouldGetLargest;\n        }\n        return sb.toString();\n    }\n\n}
[]