Created
May 4, 2016 07:37
-
-
Save AntonisFK/406e0e93ac8c703151662639eb000ec7 to your computer and use it in GitHub Desktop.
given a string with only letters, return the start and end index of the largest consecutive letters
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
function check(str){ | |
var word = str.split(""); | |
var max = 0; | |
var arr1 = []; | |
var arr2 = []; | |
var letter = 0; | |
var counter = 0; | |
for(var i=0; i< word.length; i++){ | |
if(word[letter] === word[i]){ | |
counter ++; | |
}else{ | |
arr1.push(counter); | |
arr2.push([letter, i]); | |
counter = 0; | |
letter = i; | |
i = i -1; | |
} | |
} | |
console.log(arr1, arr2) | |
// find max of array 1 | |
for(var b=1; b<arr1.length; b++){ | |
if(arr1[max] < arr1[b]){ | |
max = b; | |
} | |
} | |
return arr2[max]; | |
} | |
var str = "aaannnbbsssnnwwqqqqqqqqqppppwww"; | |
check(str); | |
//=> [ 15, 24 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment