Separate the Numbers

  • + 0 comments

    Python

    def separateNumbers(s):
        is_beautiful = False
    
        curr = s[0]
        first = curr
        next_start_idx = len(curr)
        while len(curr) <= len(s) // 2 and not is_beautiful:
            next_assumpted = str(int(curr) + 1)
            next_actual = s[next_start_idx:next_start_idx + len(next_assumpted)]
            if next_actual == next_assumpted:
                if next_start_idx + len(next_actual) == len(s):
                    is_beautiful = True
                else:
                    curr = next_actual
                    next_start_idx += len(curr)
    
            else:
                curr = s[:len(curr) + 1]
                first = curr
                next_start_idx = len(curr)
    
        print(s, f'YES {first}' if is_beautiful else 'NO')