We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticvoidseparateNumbers(Strings){if(s.length()==1){System.out.println("NO");//cant be splitreturn;}intsize=1;//start with size 1//check string for each starting sizewhile(size<s.length()/2+1){StringstartString=s.substring(0,size);longstartVal=Long.parseLong(startString);//start val of generated vals//generate list of numbers for current starting sizeStringBuildergenVals=newStringBuilder();longcurrentVal=startVal;//stores current val being appended to genvalswhile(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 stringif(s.equals(genVals.toString())){//first x will always be the smallest, no need for extra iterationsSystem.out.println("YES "+startVal);return;}size++;}//no beautiful string was foundSystem.out.println("NO");}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Separate the Numbers
You are viewing a single comment's thread. Return to all comments →
My Java solution: