Greedy Python Solution | 100% Runtime
Upvote if you find this article helpful\u2B06\uFE0F.\n### STEPS:\n1. Basically we make a sorted table with freq of each character.\n2. Then we take the most lex
Upvote if you find this article helpful\u2B06\uFE0F.\n### STEPS:\n1. Basically we make a sorted table with freq of each character.\n2. Then we take the most lexicographically superior character (Call it A).\n3. If its freq is in limits, directly add it.\n4. If its freq is more than the limit. Add repeatLimit number of A\'s and then search for the next lexicographically superior character(Call it TEMP) and add it just once, as we will again add the remaining A\'s.\n5. Repeat this process until either all A\'s are consumed or we run out of TEMP characters.\n6. Repeat all these steps for all other remaining characters in lexicographic order.\n\n**NOTE: Here we make the Greedy choice of choosing a lexicographically superior character at each step, while selecting A and also while selecting TEMP.**\n**NOTE: I have not removed characters if a characters freq becomes 0. This is because random pop() operations in the middle of a list takes O(n) time. For pop() operations in default index(End of list) is O(1). Refer to this article \u27A1\uFE0F [Time Complexity of Python Library Functinons](https://wiki.python.org/moin/TimeComplexity)** \n\n### STATUS:\n![image](https://assets.leetcode.com/users/images/e113bb3f-5f5a-40a4-92ff-a0049a454b4b_1645331843.2130945.png)\n\n### CODE:\n```\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        table = Counter(s)\n        char_set = [\'0\', \'a\', \'b\', \'c\', \'d\', \'e\', \'f\', \'g\', \'h\', \'i\', \'j\', \'k\', \'l\', \'m\', \'n\', \'o\', \'p\', \'q\', \'r\', \'s\',\n                    \'t\', \'u\', \'v\', \'w\', \'x\', \'y\', \'z\']\n        sorted_table = []\n        for i in range(26,-1,-1):\n            if char_set[i] in table:\n                sorted_table.append((char_set[i],table[char_set[i]]))\n\n        result = ""\n        n = len(sorted_table)\n        for i in range(n):\n            char, curr_freq = sorted_table[i] # The lexicographically larger character and its frequency\n            index_to_take_from = i + 1 # We take from this index the next lexicographically larger character(TEMP) if the repeatLimit for A is exceeded\n            while curr_freq > repeatLimit: # Limit exceeded\n                result += char*repeatLimit # Add repeatLimit number of character A\'s\n                curr_freq -= repeatLimit # Decrease frequency of character A\n                # Now we search for the next lexicographically superior character that can be used once\n                while index_to_take_from < n: # Till we run out of characters\n                    ch_avail, freq_avail = sorted_table[index_to_take_from]\n                    if freq_avail == 0: # If its freq is 0 that means that it was previously used. This is done as we are not removing the character from table if its freq becomes 0. \n                        index_to_take_from += 1 # Check for next lexicographically superior character\n                    else:\n                        result += ch_avail # If found then add that character \n                        sorted_table[index_to_take_from] = (ch_avail,freq_avail-1) # Update the new characters frequency\n                        break\n                else:\n                    break # We cannot find any lexicographically superior character\n            else:\n                result += char*curr_freq # If the freq is in limits then just add them\n        return result\n```\n
['Python', 'Python3']