Created
March 29, 2019 20:53
-
-
Save codesnakers/d8077900bc4e9117de082bfb62da92e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Solution for: http://codesnakers.com/projects/6.png | |
*/ | |
function solution(s){ | |
var len = s.length; | |
var sub = []; | |
var subLen = 0, tempLen; | |
var i=0, k = 0; | |
var isSorted = false; | |
var result; | |
for(var l=0; l<24; l++){ | |
if(s.indexOf(String.fromCharCode(97+l))>-1){ | |
i = s.indexOf(String.fromCharCode(97+l)); | |
break; | |
} | |
} | |
if(i===0){ | |
isSorted = true; | |
} | |
while(i < len){ | |
if(i>0 && s.charCodeAt(i) > s.charCodeAt(i-1)){ | |
i++; | |
continue; | |
} | |
if(i>0 && k>0){ | |
if(sub[k-1].charCodeAt(0) > s.charCodeAt(i)) k--; | |
} | |
sub[k] = s.charAt(i); | |
for(var j=i+1; j < len; j++){ | |
if(s.charCodeAt(j-1)<=s.charCodeAt(j)){ | |
sub[k] += s.charAt(j); | |
} | |
} | |
if(isSorted && i<=len-1 && s.charCodeAt(i) > s.charCodeAt(i+1)){ | |
isSorted = false; | |
} | |
i++; | |
k++; | |
} | |
if(isSorted){ | |
return 3; | |
} | |
subLen = sub[0].length; | |
result = len - subLen; | |
if(result >= 3){ | |
return result; | |
} | |
return 0; | |
} | |
solution("banana"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment