import string


class FreeCodesCalculator:
    def __init__(self, ctm_codes_set):
        self.ctm_codes_set = ctm_codes_set
        self.lastUsedCode = 'AAA'

    def get_next_free_code(self):
        while self.lastUsedCode in self.ctm_codes_set:
            self.increment_last_used_string()
        self.ctm_codes_set.add(self.lastUsedCode)
        return self.lastUsedCode

    def add_used_code(self, ctm_code: str):
        self.ctm_codes_set.add(ctm_code)

    def increment_last_used_string(self):
        # Convert the string to a list of characters for easy manipulation
        s = list(self.lastUsedCode)
        # Start from the end of the string
        i = len(s) - 1

        # Process the string from the end to the start
        while i >= 0:
            if s[i] == 'Z':
                s[i] = 'A'
                i -= 1
            else:
                s[i] = chr(ord(s[i]) + 1)
                break
        else:
            # If we processed the entire string and all characters were 'Z'
            s.insert(0, 'A')

            # Convert the list of characters back to a string
        self.lastUsedCode = ''.join(s)
