Separate the Numbers

Sort by

recency

|

673 Discussions

|

  • + 0 comments

    My answer in Typescript, accepted

    function separateNumbers(s: string): void {
        /**
         * idea is check each couple of number that can be starting number [num]
         * then check the [s] left that is the continues of [num]
         * 
         * + number [threshold]: amount of number of [num] we will take
         * + number [result]: final number we finded [num]
         */
    
        let threshold = 1
        let result = BigInt(0)
    
        // outer loop, get [num] by [threshold], increasing if [num] not beauty
        check_s: while (!result) {
            // [num] can't be large than haft of [s], mean [s] can't be beauty
            if (threshold > s.length / 2) break check_s;
    
            let num = BigInt(s.substring(0, threshold))
            let num_next = num + BigInt(1)
            let s_left = s.substring(threshold)
    
            // inner loop, keep reduce [s_left] till empty
            check_beauty: while (true) {
                if (s_left.startsWith(`${num_next}`)) {
                    s_left = s_left.replace(`${num_next}`, '')
                    num_next = num_next + BigInt(1)
                }
                // can't reduce, [s] is not beauty, break [check_beauty] inner loop
                else break check_beauty;
    
                // [s_left] empty, mean [num] is beauty, set [result = num], break [check_s] outer loop
                if (s_left.length == 0) result = num;
            }
    
            // inner loop check [num] is not beauty number, next
            threshold++;
        }
    
        // idk why console, why not return?
        console.log(result ? `YES ${result}` : 'NO')
    }
    
  • + 0 comments
    def generateSplit(s, c):
        
        exp = r'\d' * c
        r = re.compile('(' + ''.join(exp) + ')')
        
        split = list(filter(None, r.split(s))) #split the string and remove ''
        
        if c > 1 and any(map(lambda x: x[0] == '0', split)):
            return None #discard if any item in split begins with 0
            
        split = list(map(int, split))
        
        return split
        
    def separateNumbers(s):
        # Write your code here
        
        
        for i in range(1, len(s)//2 + 1):
      
            t = generateSplit(s, i)
            
            if t and all(map(lambda x: (t[x] - t[x-1]) == 1, range(1,len(t)))):
                print("YES", t[0])
                break
            
            e = '9' * i
            if e in s:
                x = s.find(e + str(int(e) + 1)) + i # find 9(10), 99(100) to split
                part1 = generateSplit(s[:x], i) 
                part2 = generateSplit(s[x:], i + 1) 
    
    
                if part1 and part2:
                    t = part1 + part2 
                    
                    if all(map(lambda x: (t[x] - t[x-1]) == 1, range(1,len(t)))):
                        print("YES", t[0])
                        break
        else:
            print("NO")
    
  • + 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;
    }
    
  • + 0 comments
    def separateNumbers(s):
        n = len(s)
        for l in range(1, n // 2 + 1):
            fir_num = int(s[:l])  
            cur_num = fir_num
            ans = str(fir_num)
            while len(ans) < n:
                cur_num += 1
                ans += str(cur_num)
            
            if ans == s:
                print(f"YES {fir_num}")
                return
        print("NO")
    
  • + 0 comments

    instead of a lot of comparisons, just take first number and build it and then check

        public static void separateNumbers(String s) {
            if (s.charAt(0) == '0') {
                System.out.println("NO");
                return;
            } else {
                for (int i = 1; i < (s.length() / 2)+1; i++) {
                    Long num = Long.valueOf(s.substring(0, i));
                    StringBuilder b = new StringBuilder(s.substring(0,i));
                    Long l = Long.valueOf(num);
                    while (b.length() < s.length()) {
                        b.append(l + 1);
                        l = l + 1;
                    }
                    if (b.toString().equals(s)) {
                        System.out.println("YES " + num);
                        return;
                    }
                }
                System.out.println("NO");
            }
        }