Separate the Numbers

Sort by

recency

|

681 Discussions

|

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

    Here is my easy C++ solution, explanation here : https://youtu.be/e32U19k1X6A

    void separateNumbers(string s) {
        int bl = 1;
        bool f = false;
        while(bl * 2 <= s.size()){
            string base = s.substr(0, bl);
            string newString = "";
            long baselong = atol(base.c_str());
            do{
                newString += to_string(baselong);
                baselong++;
            }while(newString.size() < s.size());
            if(newString == s) {cout << "YES " << base;f = true;break;}
            bl++;
        }
        if(!f) cout << "NO";
        cout << endl;
    }