Separate the Numbers

  • + 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");
        }