Problem Statement
Bear Limak loves algorithms, especially the ones with words and strings.
Limak's friend recently entered a programming competition and wrote a program. The program contains a string constant s. Limak would now like to challenge the program by making it exceed the time limit. To do that, he must find two different characters in s that are as far apart as possible.
Formally, Limak must find two integers i and j with the following properties:
- Both i and j must be valid indices into s. That is, both numbers must be between 0 and n-1, inclusive, where n is the length of s.
- The characters s[i] and s[j] must be different.
- The difference between i and j must be as large as possible.
You are given the
Definition
- Class:
- BearPair
- Method:
- bigDistance
- Parameters:
- String
- Returns:
- int
- Method signature:
- int bigDistance(String s)
- (be sure your method is public)
Constraints
- s will have between 2 and 50 characters, inclusive.
- Each character in s will be a lowercase English letter ('a' - 'z').
Examples
"bear"
Returns: 3
Limak can choose the (0-based) indices 0 and 3. We have s[0]='b' and s[3]='r', which are indeed two different letters. The difference between the two indices is 3-0 = 3.
"abcba"
Returns: 3
Here, one optimal solution is for Limak to choose the indices 1 and 4 (corresponding to 'b' and 'a', respectively). Another optimal solution is to choose indices 0 and 3 (letters 'a' and 'b'). In both cases the difference is 3.
"oooohyeahpotato"
Returns: 13
"zzzzzzzzzzzzzzzzzzzzz"
Returns: -1
Here, Limak can't choose two indices with different letters.
"qw"
Returns: 1
"xxyyxyxyyyyyyxxxyxyxxxyxyxyyyyyxxxxxxyyyyyyyyxxxxx"
Returns: 47
"qqqqqqqqqqqpasbzqcefrbmzbeeklwxjuqahqq"
Returns: 35
"piiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"
Returns: 38
"waqunmledulebqqeesuhpctgdewwwww"
Returns: 29
"wwwwwwwwwbtvyqjfjdwwwwwwwwwwwwww"
Returns: 22
"ssssssssssss"
Returns: -1
"aa"
Returns: -1
"zz"
Returns: -1
"yz"
Returns: 1
"yz"
Returns: 1
"aba"
Returns: 1
"abb"
Returns: 2
"aab"
Returns: 2
"aaa"
Returns: -1
"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"
Returns: -1
"vijjv"
Returns: 3
"qqqqqqqqqqqqkruagjeqftqqqqqqqqqqq"
Returns: 21
"srclwgmobsdiqppxjbtlkpaouzxmvvnhvmmmmmmmmmmmmm"
Returns: 45
"ooooonnhfcznftrtgxptbjoooooooooooooo"
Returns: 30
"bzmbr"
Returns: 4
"pp"
Returns: -1
"ewrpjvwkwinnoupgtrbvijxnaquwluqpqhhzedmbnbqbvikpzl"
Returns: 49
"qqqqqepaaxkzswfnssbfxkdbmtedgtcphuseeqqbeludelmnuq"
Returns: 48
"bbbbbbssxjzekhgixvzenwjpxtgqibrsahjtqbbbbbbbbbbbbb"
Returns: 43
"bb"
Returns: -1
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Returns: -1
"miraupmomiqa"
Returns: 11
"yvo"
Returns: 2
"vvvv"
Returns: -1
"pp"
Returns: -1
"tj"
Returns: 1
"ttpoozilzpxtjebarvgiylurwjjpoutfeodby"
Returns: 36
"johggvonllrwrdizeroqzpvbnkoaqvlagjstibupsomswqjoq"
Returns: 48
"ycjedfvwygrctuytlujzunyjvpumimquxdggcccccccccc"
Returns: 45
"aaaaazzzzzzzzzaaaaaaaaaaaaaaaa"
Returns: 24
"zzzzzzzzzzzaaaaaaaazzzzzzzzzzzzzz"
Returns: 21
"zyz"
Returns: 1
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Returns: -1
"abaaa"
Returns: 3
"abaaaaaaaa"
Returns: 8
"aaaaaaaabbba"
Returns: 10
"xxxxxxxxxxxxy"
Returns: 12
"abz"
Returns: 2
"bbbabbbbbbbbbb"
Returns: 10
"abcdab"
Returns: 5
"zzzazz"
Returns: 3
"aaaa"
Returns: -1
"aaaaa"
Returns: -1
"bbbbbbbbbbbbbbbbbab"
Returns: 17
"aaaaaa"
Returns: -1
"ab"
Returns: 1
"adbaaa"
Returns: 4