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.
functionseparateNumbers(s:string):void{/** * idea is check each couple of number that can be starting number [num] * then check the [s] left that is the continues of [num] * * + number [threshold]: amount of number of [num] we will take * + number [result]: final number we finded [num] */letthreshold=1letresult=BigInt(0)// outer loop, get [num] by [threshold], increasing if [num] not beautycheck_s:while(!result){// [num] can't be large than haft of [s], mean [s] can't be beautyif(threshold>s.length/2)breakcheck_s;letnum=BigInt(s.substring(0,threshold))letnum_next=num+BigInt(1)lets_left=s.substring(threshold)// inner loop, keep reduce [s_left] till emptycheck_beauty:while(true){if(s_left.startsWith(`${num_next}`)){s_left=s_left.replace(`${num_next}`,'')num_next=num_next+BigInt(1)}// can't reduce, [s] is not beauty, break [check_beauty] inner loopelsebreakcheck_beauty;// [s_left] empty, mean [num] is beauty, set [result = num], break [check_s] outer loopif(s_left.length==0)result=num;}// inner loop check [num] is not beauty number, nextthreshold++;}// idk why console, why not return?console.log(result?`YES${result}`:'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 answer in Typescript, accepted