## The challenge#

For a given string `s` find the character `c` (or `C`) with longest consecutive repetition and return:

``````Object[]{c, l};
``````

where `l` (or `L`) is the length of the repetition. If there are two or more characters with the same `l` return the first in order of appearance.

For empty string return:

``````Object[]{"", 0}
``````

## Test cases#

``````import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
@Test
public void exampleTests() {
assertArrayEquals(new Object[]{"a", 4}, Solution.longestRepetition("aaaabb"));
assertArrayEquals(new Object[]{"a", 4}, Solution.longestRepetition("bbbaaabaaaa"));
assertArrayEquals(new Object[]{"u", 3}, Solution.longestRepetition("cbdeuuu900"));
assertArrayEquals(new Object[]{"b", 5}, Solution.longestRepetition("abbbbb"));
assertArrayEquals(new Object[]{"a", 2}, Solution.longestRepetition("aabb"));
assertArrayEquals(new Object[]{"", 0}, Solution.longestRepetition(""));
}
}
``````

## The solution in java#

Option 1:

``````public class Solution {
public static Object[] longestRepetition(String s) {

int len = s.length();
int count = 0;

if (len==0) return new Object[]{"", 0};

char res = s.charAt(0);
for (int i=0; i<len; i++) {
int cur_count = 1;
for (int j=i+1; j<len; j++) {
if (s.charAt(i) != s.charAt(j)) break;
cur_count++;
}

if (cur_count > count) {
count = cur_count;
res = s.charAt(i);
}
}

return new Object[]{Character.toString(res), count};
}
}
``````

Option 2:

``````public class Solution {

public static Object[] longestRepetition(String s) {
char lastch = '\0';
Object ret[] = new Object[]{"", 0};
int n = 0, max = 0;
for (char c : s.toCharArray()) {
n = lastch == c ? ++n : 1;
if (n > max) { ret = new Object[]{""+c,n}; max = n; }
lastch = c;
}
return ret;
}

}
``````