Character with longest consecutive repetition



i think i have wirtten the correct code for the problem only one thing and it that i return the first longest sequence how can i alter that to return the last maximum sequence?

an example from codewars editor : for input ‘00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz’

  • Expected: [‘c’, 19], instead got: [‘0’, 19]

here is my code:

function longestRepetition(s) {

 var count = 0;
 var temp = s.charAt(0);
  var arr = [];
for (var i = 0; i < s.length; i++) {
      if (temp === s.charAt(i)) {
      count++
      temp = s.charAt(i)
       }
      else {
      temp = s.charAt(i);
      arr.push(count)
      count = 1;
       }
   if(i==s.length-1)
    arr.push(count);
    }
   if(arr.length>0)
   {  
    var Max=arr[0]
    for(var i=0;i<arr.length;i++)
    {
      if(Max<=arr[i]) 
      Max=arr[i];
   }
    }

  else var Max=0;
    var mindex=arr.indexOf(Max);

 return [s.charAt(mindex),Max]

    }

Answer

The issue in your code is that minindex is an index in your arr, but that index has nothing to do with s. So s.charAt(minindex) makes no sense. You should maintain for which character you had found the count. For instance you could push in arr both the count and the corresponding character (as a subarray with two values). Then the rest of your code would only need little modification to make it work.

Applying this idea to your code without changing anything else, we get this:

function longestRepetition(s) {
    var count = 0;
    var temp = s.charAt(0);
    var arr = [];
    
    for (var i = 0; i < s.length; i++) {
        if (temp === s.charAt(i)) {
            count++
            temp = s.charAt(i) // Not necessary: was already equal
        }
        else {
            arr.push([temp, count]); // <--- pair, BEFORE changing temp
            temp = s.charAt(i);
            count = 1;
        }
        if(i==s.length-1)
            arr.push([temp, count]); // <---
    }

    if(arr.length>0)
    {  
        var Max=arr[0]; // <-- Max is now a pair of char & count
        for(var i=0;i<arr.length;i++)
        {
            if(Max[1]<arr[i][1])  // Comparison changed to just less-than
                Max=arr[i];
        }
    }
    else Max=[null, 0]; // Must be a pair here also

    return Max; // Just return the pair
}

console.log(longestRepetition('00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz'));

But you can do the same with less code:

function longestRepetition(s) {
    let result = [null, 0]; // pair of character and count
    for (var i = 0; i < s.length; null) {
        let start = i++;
        while (i < s.length && s[i] === s[start]) i++; // Find end of series
        if (i - start > result[1]) result = [s[start], i - start];
    }
    return result;
}
console.log(longestRepetition('00000000000000111111111111111112222222222222223333333333333344444444444445555555555555666666666666777777777777888888888888888999999999999999999aaaaaaaaabbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeefffffffffffffggggggggggggggghhhhhhhhhhhhhiiiiiiiiiijjjjjjjjjjjjjjkkkkkkkkkkkkllllllllllmmmmmmmmmmnnnnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqrrrrrrrrrrrrrrrrrrrssssssssssttttttttttttuuuuuuvvvvvvvvvvvvvvvvvwwwwwwwwwwwwwwwwxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyzzzzzzzzzzzzzz'));


Source: stackoverflow