Separate the Numbers

Sort by

recency

|

676 Discussions

|

  • + 0 comments
    a = s[0]
        for i in range(1, len(s)):
            if s[i] == '0':
                a += '0'
            else:
                dau=s[:i]
                break
        i = 2
        while len(a) < len(s) and i <= len(s) // 2+1:
            j = int(a) + 1
            while True:
                a += str(j)
                if len(a) > len(s):
                    break
                if a == s:
                    print("YES", int(dau))
                    return
                j += 1
            if a == s:
                print("YES", int(dau))
                return
            a = s[:i]
            dau=s[:i]
            i += 1
        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

    Here is my PHP solution

    s); arr[0];

        if(count(`$arr) == 1 || $`initial == 0)
        {
              $ans = 'NO';   
        }
        else
        {
    
        $ans = 'NO';
        `$len = count($`arr);
        `$iteration = floor($`len/2)+1;
    
         $firstNum = 0;
        for (`$i=$`iteration; `$i > 0; $`i--) { 
    
            `$str = substr($`s, 0,$i);
            `$strlen = strlen($`str);
            `$second = substr($`s, `$i, $`strlen);
            `$third = substr($`s, `$i, $`strlen+1);
    
              if(((int)`$second - (int)$`str == 1)||((int)`$third - (int)$`str == 1))
              {
    
                `$firstNum = $`str;
                break;
              }
        }
    
         `$ans = 'YES  '.$`firstNum;
        `$newString = substr($`s, strlen((string) $firstNum));
    
    
    
        `$result = (int)$`firstNum;
        while ($newString!=='') {
              `$result = $`result+1;
    
                if (substr(`$newString, 0, strlen($`result)) == $result) {
    
    
                 `$newString = substr($`newString, strlen((string) $result));
    
              }
              else
              {
    
                 $ans = 'NO';
                 break;
              }
        }
    
        }
    
  • + 0 comments

    Simple implementation is sufficient. But take care of integer data type overflow.

  • + 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')
    }