Separate the Numbers

Sort by

recency

|

667 Discussions

|

  • + 0 comments

    My C code 😎😁

    void separateNumbers(char* s) {
        int len = strlen(s);
        if(len == 1){
            printf("NO\n");
            return;
        }
        
        for(int i = 1;i <= len /2 ;i++){
            char t[32];
            strncpy(t,s,i);
            t[i] = '\0';
            long long firstNum = atoll(t);
            
            char tempStr[1024];
            snprintf(tempStr,sizeof(tempStr),"%lld",firstNum);
            long long nextNum = firstNum + 1;
            while(strlen(tempStr) < len){
                char buffer[32];
                snprintf(buffer,sizeof(buffer),"%lld",nextNum);
                strcat(tempStr,buffer);
                nextNum++;
            }
            
            if(strcmp(tempStr,s) == 0){
                printf("YES %lld\n",firstNum);
                return;
            }
        }
        printf("NO\n");
    }
    
  • + 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

    C#

    public static void separateNumbers(string s)
    {
        var print = "NO";
    
        if (s.Length > 1)
        {
            var sequenceStart = string.Empty;
    
            for (int i = 0; i < s.Length / 2; i++)
            {
                sequenceStart += s[i];
                var num = long.Parse(sequenceStart);
                var sequence = string.Empty;
    
                while (s.Length > sequence.Length)
                {
                    if (!s.StartsWith(sequence)) break;
    
                    sequence += num;
                    ++num;
                }
    
                if (s.StartsWith(sequence))
                {
                    print = $"YES {sequenceStart}";
                }
    
                if (s.Length == sequence.Length) break;
            }
        }
    
        Console.WriteLine(print);
    }
    
  • + 0 comments
    void separateNumbers(string s) {
        int size = s.size();
        for (int i = 1; i <= size / 2; i++) {
            string t = s.substr(0, i);
            string n = t;
            long long count = stoll(t);
            while (t.size() < size) {
                count++;
                t += to_string(count);
            }
            if (t == s) {
                cout << "YES " << n << endl;
                return;
            }
        }
        cout << "NO" << endl;
    }
    
  • + 0 comments

    Haskell

    More haskell-y than necessary, but fun. Relies on the max input string length of 32.

    module Main where
    
    import Data.List (find, inits)
    import Data.Set qualified as S
    
    -- problem constraint
    maxsize :: Int
    maxsize = 32
    
    -- seed & iterations
    newtype BeautifulString = BeautifulString (Int, Int)
    
    -- conversion to string
    instance Show BeautifulString where
        show (BeautifulString (seed, iterations)) = concatMap show [seed .. seed + iterations - 1]
    
    -- all BSs with seed and net string length <= maxsize
    validBSs :: Int -> S.Set String
    validBSs seed =
        let bss = [show $ BeautifulString (seed, k) | k <- [2 ..]]
         in S.fromList $ takeWhile (\bs -> length bs <= maxsize) bss
    
    -- if string is beautiful, return the seed
    isBeautiful :: String -> Maybe Int
    isBeautiful "" = Nothing
    isBeautiful ('0' : _) = Nothing
    isBeautiful s =
        let stemStrings = tail $ inits s -- first is ""
            halflen = length s `div` 2
            validSeeds = map read $ takeWhile (\stem -> length stem <= halflen) stemStrings
         in find (S.member s . validBSs) validSeeds
    
    -- display result in required format
    showIsBeautiful :: String -> String
    showIsBeautiful s =
        case isBeautiful s of
            Just n -> "YES " <> show n
            Nothing -> "NO"
    
    -- process input list, ignoring the first line (the count)
    main :: IO ()
    main = getContents >>= mapM_ (putStrLn . showIsBeautiful) . tail . lines