Skip to content

Commit

Permalink
Implement longestRun.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 10, 2015
1 parent 063a494 commit 8ff52c2
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion longestRun/longestRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,32 @@
*/

var longestRun = function (string) {
// TOD: Your code here!
var strLen = string.length;
if (strLen === 0) return [];
if (strLen === 1) return [0, 0];

// strings of length 2 or greater
var currentStart = 0;
var currentLength = 1;
var maxStart = 0;
var maxLength = 1;
var i;

for (i = 1; i < strLen; i += 1) {
// check if this character is the same as previous character
if ( string[i] === string[i - 1] ) {
currentLength += 1;
} else {
if (currentLength > maxLength) {
maxStart = currentStart;
maxLength = currentLength;
}
currentStart = i;
currentLength = 1;
}
}

return [ maxStart, maxStart + maxLength - 1 ];
};

// If you need a random string generator, use this!
Expand Down

0 comments on commit 8ff52c2

Please sign in to comment.