Separate the Numbers

Sort by

recency

|

94 Discussions

|

  • + 0 comments

    Python

    def separateNumbers(s):
        is_beautiful = False
    
        curr = s[0]
        first = curr
        next_start_idx = len(curr)
        while len(curr) <= len(s) // 2 and not is_beautiful:
            next_assumpted = str(int(curr) + 1)
            next_actual = s[next_start_idx:next_start_idx + len(next_assumpted)]
            if next_actual == next_assumpted:
                if next_start_idx + len(next_actual) == len(s):
                    is_beautiful = True
                else:
                    curr = next_actual
                    next_start_idx += len(curr)
    
            else:
                curr = s[:len(curr) + 1]
                first = curr
                next_start_idx = len(curr)
    
        print(s, f'YES {first}' if is_beautiful else 'NO')
    
  • + 0 comments

    My Java Solution:

    public static void separateNumbers(String s) { // Write your code here for(int i = 1; i <= s.length()/2; i++) {

            long firstNumber = Long.parseLong(s.substring(0, i));
            long currentNumber = firstNumber;
    
            StringBuilder shot = new StringBuilder();
            shot.append(currentNumber);
    
    
            while(shot.length() < s.length()){
                currentNumber++;
                shot.append(currentNumber);
            }
    
            if(s.equals(shot.toString()))
            {
                System.out.println("YES " + firstNumber);
                return;
            }     
        }
        System.out.println("NO");
    
    }
    

    }

  • + 0 comments

    This is my solution in javascript:

    let i = 0;
    let beautiful = false;
    let head = 0;
    
    while (i < s.length - 1) {
      let currentChar = BigInt(s.slice(0, i + 1));
      let newString = currentChar.toString();
      head = currentChar;
      while (newString.length < s.length) {
        currentChar += BigInt(1);
        newString += currentChar.toString(); // console.log(newString);
      }
      if (newString === s) {
        beautiful = true;
        break;
      } else {
        i++;
      }
    }
    
    if (beautiful) {
      console.log("YES " + head);
    } else {
      console.log("NO");
    }
    
  • + 0 comments

    My rust solution:

    fn separateNumbers(s: &str) {
        let mut i = 0;
        'main: while i < s.len() / 2 {
            i += 1;
            let x: i64 = s[..i].parse().unwrap();
            
            let mut next = (x + 1).to_string();
            if s[i..(i + next.len())] == next {
                let mut tmp = &s[(i + next.len())..];
                while !tmp.is_empty() {
                    next = (next.parse::<i64>().unwrap() + 1).to_string();
                    if &tmp[..next.len().min(tmp.len())] != next {
                        continue 'main;
                    }
                    tmp = &tmp[next.len()..];
                }
                println!("YES {x}");
                return;
            }
        }
        println!("NO");
    
  • + 0 comments

    C# solution.

            {
                int start = 0;
                bool f = false;
                int noOfDigits = 1;
                long firstItemOfSequence = 0;
                string string_modified = str;
                while (start <= str.Length)
                {
                    string firstAsString = str.Substring(start, noOfDigits);
                    int currentPosition = firstAsString.Length + str.IndexOf(firstAsString);
                    long firstAsNumber = long.Parse(firstAsString);
                    if (firstItemOfSequence == 0) firstItemOfSequence = firstAsNumber;
                    long secondNumber = firstAsNumber + 1;
                    str = str.Substring(currentPosition, str.Length - currentPosition);
                    if (str.IndexOf(secondNumber.ToString()) == 0)
                    {
                        start = str.IndexOf(secondNumber.ToString());
                        f = true;
                        currentPosition = secondNumber.ToString().Length + str.IndexOf(secondNumber.ToString());
                        noOfDigits = secondNumber.ToString().Length;
                    }
                    else if (firstAsString.Length >= string_modified.Length / 2) break;
                    else
                    {
                        f = false;
                        start = 0;
                        firstItemOfSequence = 0;
                        noOfDigits++;
                        str = string_modified;
                    }
                    if (currentPosition == str.Length) break;
                }
                if (f) Console.WriteLine($"YES {firstItemOfSequence}");
                else Console.WriteLine("NO");
    
            }