Write a function that, given a string, finds the longest run of identical characters.
Returns an array containing the start and end indices of that run.
If there are two runs of equal length, return the first one.
For example:
longestRun("abbbcc") // [1, 3] longestRun("aabbc") // [0, 1] longestRun("abcd") // [0, 0] longestRun("") // [0, 0]
var longestRun = function (string) { var stringArr = string.split(""); var repeatedChars = ['empty',0]; var longestRepeatedChar = [0,0]; for(var i = 0; i < stringArr.length; i++) { if(stringArr[i] === stringArr[i + 1]) { if(repeatedChars[0] === 'empty') { repeatedChars[0] = i;/*from ww w.jav a2 s. co m*/ } } else if(repeatedChars[0] !== 'empty') { repeatedChars[1] = i; console.log('repeated ', repeatedChars, 'longest ', longestRepeatedChar); if(repeatedChars[1] - repeatedChars[0] > longestRepeatedChar[1] - longestRepeatedChar[0]) { longestRepeatedChar[0] = repeatedChars[0]; longestRepeatedChar[1] = repeatedChars[1]; repeatedChars[0] = 'empty'; repeatedChars[1] = 0; } } } return longestRepeatedChar; }; console.log(longestRun("abbbcc")); console.log(longestRun("aabbc")); console.log(longestRun("abcd")); console.log(longestRun(""));