You are viewing a single comment's thread. Return to all comments →
Here's my JS solution. I started from the end of the string and worked backwords until I found a character that is less than something previous:
let a = `abcdefghijklmnopqrstuvwxyz`; let idVals = [[w.length-1, a.indexOf(w.charAt(w.length-1))]]; for(let i = w.length-2; i >= 0; i--){ let val = a.indexOf(w.charAt(i)); if(val < idVals[idVals.length-1][1]){ let sub = ``; for(let j = 0; j < idVals.length; j++){ let id = idVals[j][0]; if(val < idVals[j][1]){ let start = (i === 0) ? w.charAt(id) : w.substring(0,i) + w.charAt(id); sub += w.substring(i,id).split(``).sort().join(``); console.log(sub); return start + sub; } sub += w.charAt(id); } } idVals.push([i,val]); } return `no answer`;
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
Here's my JS solution. I started from the end of the string and worked backwords until I found a character that is less than something previous: