Separate the Numbers

Sort by

recency

|

677 Discussions

|

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

    My Java solution:

    public static void separateNumbers(String s) {
            if(s.length() == 1){
                System.out.println("NO"); //cant be split
                return;
            }
            
            int size = 1; //start with size 1
            
            //check string for each starting size
            while(size < s.length() / 2 + 1){
                String startString = s.substring(0, size);
                long startVal = Long.parseLong(startString); //start val of generated vals
                
                //generate list of numbers for current starting size
                StringBuilder genVals = new StringBuilder();
                long currentVal = startVal; //stores current val being appended to genvals
                while(genVals.length() < s.length()){
                    genVals.append(currentVal);
                    currentVal++;
                }
                //print the first val of the beautiful strirng if the numerical concatenation is equal to the og string
                if(s.equals(genVals.toString())){
                    //first x will always be the smallest, no need for extra iterations
                    System.out.println("YES " + startVal);
                    return;
                }
                
                size++;
            }
            //no beautiful string was found
            System.out.println("NO");
        }
    
  • + 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 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.