Untitled

 avatar
unknown
plain_text
5 months ago
1.3 kB
3
Indexable
def is_attractive(code, k):
    n = len(code)
    for i in range(n - k):
        if code[i] != code[i + k]:
            return False
    return True

def generate_attractive_code(org_code, k):
    n = len(org_code)
# If original code is already attractive, return it
if is_attractive(org_code, k):
    return org_code

# Convert to list for easier manipulation
code_list = list(org_code)

# Start from left, ensure pattern compliance
for i in range(n):
    # If we're at a position that should match previous pattern
    if i >= k:
        # Must match the character k positions before
        code_list[i] = code_list[i - k]
    else:
        # For first k positions, we can choose digits
        # If current digit is less than original, we need to increment
        if ''.join(code_list) < org_code:
            # Try incrementing current digit
            curr_digit = int(code_list[i])
            for d in range(curr_digit + 1, 10):
                code_list[i] = str(d)
                # Propagate pattern
                pos = i + k
                while pos < n:
                    code_list[pos] = code_list[pos - k]
                    pos += k
                if ''.join(code_list) >= org_code:
                    break

return ''.join(code_list)

Test cases
Editor is loading...
Leave a Comment