Separate the Numbers

  • + 0 comments

    Here is a O(n^2) time and and O(n) space:

    O(n^2) time because in the worse case scenario the outer loop will run through the entires string by 1/2 and the while loop will run through all charecters in the string. O(n) space because the string created will be as long as 's'

    def separateNumbers(s):
        start = ""
        
        for i in range(1, (len(s) // 2 + 1)):
            start = s[:i]
            num = int(start)
            res = start
            
            while len(res) < len(s):
                num += 1
                res += str(num)
                
                if s[:len(res)] != res:
                    break
            
            if s == res:
                print(f"YES {start}")
                return
        
        print("NO")