Separate the Numbers

Sort by

recency

|

682 Discussions

|

  • + 0 comments

    My python sollution :

    def separateNumbers(s):
        for i in range(len(s)//2):
            n = s[:(i+1)]
            l = 0
            while l < len(s):
                t = str(int(n)+1)
                if s.find(t,l) != -1 :
                    if s.find(n,l) + len(n) != s.find(t,l) :
                        break
                    else :
                        l += len(n)
                        n = t
                else:
                    l += len(n)
                    break
            if l == len(s):
                print(f'YES {s[:(i+1)]}')
                return
        else :
            print('NO')
    
  • + 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")
    
  • + 0 comments

    Here is problem solution in python, java c++ c and javascript- https://programmingoneonone.com/hackerrank-separate-the-numbers-solution.html

  • + 0 comments

    thanks

  • + 0 comments
    #!/bin/python3
    
    from collections import deque
    
    def separateNumbers(s):
        d = 1
        n = len(s)
        
        while d < n:
            dq = deque(list(s[d:]))
            first = s[:d]
            a = s[:d]
            b = ""
            while dq:
                if not a or a[0] == '0':
                    break
                    
                if all(n == '9' for n in a):
                    d += 1
                
                for _ in range(d):
                    if dq:
                        b += dq.popleft()
                
                if not b or b[0] == '0' or int(b) - int(a) != 1:
                    break
                    
                if not dq:
                    print(f"YES {first}")
                    return
                else:
                    a, b = b + '', ''
                    
            d = len(first) + 1
        
        print("NO")
        return