Separate the Numbers

Sort by

recency

|

103 Discussions

|

  • + 0 comments

    c++

    void separateNumbers(string s) {
        long long number, next;
        string new_string;
        bool possible = false;
        // try to build the beautiful string starting with the first number, if it's not possible increase the number of digits and try again
        for (int digits = 1; digits <= s.size()/2; digits++) {
            // get the first number with the current number of digits from the string given
            // also keep the number for later to print if it's good
            number = stoll(s.substr (0, digits));
            // get the next number, for beautiful condition
            next = number + 1;
            // concatenate numbers with the next beautiful number until the minimum length is achieved
            new_string = s.substr(0, digits) + to_string(next);
            while (new_string.size() < s.size()) {
                next++;
                new_string += to_string(next);
            }
            // if the length is equal and the number is the same, it's possible
            if (new_string.size() == s.size() && new_string == s) {
                possible = true;
                // early exit, no need to continue checking
                break;
            }
        }
        // print result
        if (possible) {
            cout<<"YES "<<number<<endl;
        } else {
            cout<<"NO"<<endl;
        }
    }
    
  • + 0 comments

    If you understand the main idea behind this problem, then the solution is easy. We have to figure out two things:

    1. The correct starting number (first_num)
    2. Whether the string forms a beautiful sequence from that starting number.

    Algo:

    Iterate through the string and try every possible starting number by taking its prefix.

    *for i in range(1, len(s) // 2 + 1):  # Try every possible length for firstnum
        firstnum = int(s[:i])           # Guess the first number
        nextnum = firstnum + 1         # The next expected number in sequence
        temp = s[:i]                     # Start building the sequence from firstnum*
    

    Now simulate building the sequence by adding next numbers:

    *
        while len(temp) < len(s):        # While our built string is shorter than the original
            temp += str(nextnum)        # Append the next number
            nextnum += 1                # Move to the next*
    

    Finally, check if the built string matches the original:

    *    if temp == s:
            print("YES", firstnum)
            return
    *
    

    If no such starting number worked, then the string isn’t beautiful:

    print("NO")

  • + 1 comment

    I spent like 10 hours trying to do this the hard way. When I figured out the easy way I did it in 5 minutes during a business meeting. Do yourself a favor:

    For input "99100", grab the first digit 9 and build beautiful string 91011, and check whether it's identical to 99100, It's not. So grab the first 2 digits 99 and build beautiful string 99100. It's identical, so "YES 99".

    Don't try to stairstep a left index and a right index or dynamically keep track of curernt index length or anything. Save it for the harder questions lmao.

  • + 0 comments

    I have no clue in whos vocab is this a Basic - Easy questions. This is more like a medium question and I would say a harder one of that.

    I have used a backtracking kind approach. Try diff length numbers to begin the recursive calls, then check if the next number we can come up with is +1 of previous. If we find a number like that we have another recursive call.| If the number is too big we can stop and return false. If the starting number size is already more than half the digits count we can also stop.

  • + 1 comment

    Let's talk about constraints...

    The constraints listed on this problem are EXTREMELY MISLEADING! There was a crazy amount of guessing on this problem to figure out which constraints the hackerrank brain was assuming since the ones written did not work.