Problem Statement
An election has just finished. There were two candidates: A and B. Each voter voted for one of the two candidates. The winner will be the candidate who received more votes. Thus, there are three possibilities for the outcome: A won, B won, or the election is a tie.
You are a journalist who observes the election committee. Your task is to report the outcome of the election as quickly as possible.
The votes cast are given in the
The election committee is counting the votes in the order in which they are given in the string. Luckily, you just realized that you probably won't have to be there until the very end. It is quite likely that you will already know the outcome of the election before the last vote has been counted.
For example, suppose the votes cast are votes = "AABAABAAB". The election committee will count the votes in this order: one vote for A, another vote for A, then a vote for B, and so on. After they count the first seven votes ("AABAABA") you will already know the election result. (Candidate A is leading with five votes to two, and the remaining two votes can no longer change the winner of the election.)
Return the smallest X such that the result of the election will be known after the first X votes have been counted.
Definition
- Class:
- BallotCounting
- Method:
- count
- Parameters:
- String
- Returns:
- int
- Method signature:
- int count(String votes)
- (be sure your method is public)
Constraints
- votes will have between 1 and 50 characters, inclusive.
- Each character in votes will be 'A' or 'B'.
Examples
"AAAAABBBBB"
Returns: 10
You have to wait until all ten votes have been counted. The final result is a tie.
"AAAAABBBBA"
Returns: 10
Again, you have to wait until all ten votes have been counted. After nine votes the counting is in exactly the same state as in Example 0: A has 5 votes, B has 4 votes, and one vote remains to be counted. In this case, A got the last vote and won the election.
"AAAAA"
Returns: 3
This election is a landslide. After we see three of the five votes, we are already sure that A will be the winner.
"AAAAAA"
Returns: 4
This is very similar to the previous example. Note that after three votes it is still possible that the election will be a tie, we have to see the fourth A to be sure.
"AAABAA"
Returns: 5
"AABAABAAB"
Returns: 7
This is the example from the problem statement.
"A"
Returns: 1
"B"
Returns: 1
"BB"
Returns: 2
"BABABBB"
Returns: 6
Here B is the winner of the election and we will know it before we see the last vote.
"BBBBBBBBBBABBABABBBBABBB"
Returns: 15
"BBBBAABBBBBBBBBB"
Returns: 11
"BAABABBBBBBABA"
Returns: 11
"ABBABBBBBABABABBBABABBBABBABABABBABBABAABBBAAA"
Returns: 36
"AAAABAAABAABABBAAAAAAABAAAAAAABAAA"
Returns: 24
"BBABBBABABABBBBBBBBBBBBABAB"
Returns: 18
"ABABABAAAAA"
Returns: 9
"BBBBBBBBBBBBBBBBBBABB"
Returns: 11
"ABBBBAABABBBABBBBAABBBABAAAA"
Returns: 22
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
Returns: 16
"BABBBABBAABABABAAABBBAAABABBAAAAABBABBBABAABBAABB"
Returns: 49
"BAAAAAAAABAABBBAABABBABBBBBBBBBABBA"
Returns: 33
"BABA"
Returns: 4
"AABABAABABABAAABBBBABABAAABABABAB"
Returns: 30
"ABAABBAABAAAAABBABBAABAAAABABABBA"
Returns: 26
"BBBBBABABBABBABBBABBBABBBABAABAAAA"
Returns: 24
"ABAAAABAAAA"
Returns: 8
"BABABABAAAABAAAABBBA"
Returns: 16
"BBBBAAAABAABABAAAAABAAABBAAAABAABA"
Returns: 28
"BBBBBBAAABABBAABABAAABBAABBBAAAABAAAAABBABAB"
Returns: 43
"BABBBBBAABBBBBAABAABABBBBABBBBBBAABAAAAABBAA"
Returns: 32
"BB"
Returns: 2
"BA"
Returns: 2
"BAABABBBBABAAAAAABABBAABBABBBAAABBBB"
Returns: 36
"BBBBBBBBBBBBBBBBBBBBBBBB"
Returns: 13
"BBB"
Returns: 2
"BBAABBBBBBBABB"
Returns: 10
"AAAAAAAAAAAAAAAAAAAAABAAAAAAABAAAAAAAAAAAAAAAAAA"
Returns: 26
"BAABABAAABBBAAABAAAAABBBAABAABBAB"
Returns: 28
"BABB"
Returns: 4
"AAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAA"
Returns: 20
"BABABBAABAAAABAA"
Returns: 15
"AAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA"
Returns: 22
"ABBAAABABAABAAAAAAAABA"
Returns: 17
"AABAAAAAAABAAAAAAABAAAAABAAAAAAAAAABAAABA"
Returns: 24
"BBBBBBBBABBBBBBBBBAABBBBBBBBBBBBBBBBBBBBBBB"
Returns: 25
"AAAAAAAAAAAAAAAAA"
Returns: 9
"BBBBBABBBAABBBBBBABBBBBBBBBBBABBBB"
Returns: 22
"AAABABAABBBABABAABBBBABBAABABBB"
Returns: 30
"ABBBBBABABBBB"
Returns: 10
"ABBBBBBBBBBBBBBBBABBBBAABBBBBBBBABBBBBB"
Returns: 22
"BAABAABABAAAABB"
Returns: 12
"BABBAAAAAAAAA"
Returns: 10
"BBBBBBABABBBBBBBBBBAB"
Returns: 13
"AAAAAAAAAAAAAA"
Returns: 8
"ABABBBBBBBBBA"
Returns: 9
"ABBBBBBAABBBBBBBABABA"
Returns: 14
"AAAAAAABAAAAABAAAAAAAAAAAABAAAAAAAAAAABAA"
Returns: 23
"AAAAAA"
Returns: 4
"AAABAAA"
Returns: 5
"BAAAAAAAAAAA"
Returns: 8
"AAABBB"
Returns: 6
"ABABAABBBBBAABABBABBABABABBBAABBABA"
Returns: 31
"AABB"
Returns: 4
"AAAABB"
Returns: 4
"ABAAB"
Returns: 4