Separate the Numbers

Sort by

recency

|

101 Discussions

|

  • + 0 comments

    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.

  • + 0 comments
    def separateNumbers(s):
        # Write your code here
        i=1
        while i-1 < len(s)//2:
            d=int(s[:i])
            if rec(d,s[i:]):
                print("YES "+s[:i])
                return
            else:
                i+=1
        print("NO")
        return
                
    def rec(d,s):
        l = len(str(d+1))
        if len(s) < l:
            return False
        d_ = int(s[:l])
        if d_ == d+1:
            if len(s[l:])>0:
                return rec(d_,s[l:])
            else:
                return True
        else:
            return False
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    pub fn separate_numbers(s: &str) {
        //Time complexity: O(n^2)
        //Space complexity (ignoring input): O(n)
        //Python solution is way cleaner, made this one first and got lazy to rewrite
        let len_s = s.len();
        if len_s == 1 {
            println!("NO");
            return;
        }
        let vec_chars = s.chars().collect::<Vec<char>>();
        let mut start_number_size = 1;
        let mut number_size = start_number_size;
        let mut index: usize = start_number_size;
        let mut last_number = vec_chars[0]
            .to_string()
            .parse::<i64>()
            .expect("To have a number");
        let mut first_number = last_number;
        loop {
            if (last_number + 1) == (10u64.pow(number_size as u32) as i64) {
                number_size += 1;
            }
            let number = if number_size + index > len_s {
                last_number + 2
            } else {
                vec_chars[index..(number_size + index)]
                    .iter()
                    .collect::<String>()
                    .parse::<i64>()
                    .expect("To have a number")
            };
            if ((number - last_number) != 1) || (index + number_size > len_s) {
                start_number_size += 1;
                if start_number_size > (len_s + 1) / 2 {
                    println!("NO");
                    return;
                };
                number_size = start_number_size;
                index = 0;
                if vec_chars[0] == '0' {
                    println!("NO");
                    return;
                }
                last_number = vec_chars[0..start_number_size]
                    .iter()
                    .collect::<String>()
                    .parse::<i64>()
                    .expect("To have a number");
                first_number = last_number;
            } else {
                last_number = number;
            }
            index += number_size;
            if index == len_s {
                println!("YES {first_number}");
                return;
            }
        }
    }