Separate the Numbers

Sort by

recency

|

683 Discussions

|

  • + 0 comments

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

  • + 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