Lead changes
- O(n) · Medium
- Full plan
- Python
- JavaScript
- strings
- loops
- conditionals
Problem
"And the lead changes hands once again," says the commentator of the volleyball match. She keeps count of those changes, and now it is your job. You get a text with the points in order, made only of uppercase A and B: each letter is one point for that team. It may be empty.
After each point, the team with more points is ahead. There is a lead change when a team goes ahead and the last team that had been ahead was the other one. The first team to go ahead does not count as a change. A tie does not count either: if after a tie the same team that was winning goes ahead again, there was no change.
Return how many changes there were, as a whole number. With "ABBAAB": A ahead, tie, B ahead (1), tie, A ahead (2), tie. You return 2.
Examples
The example
"ABBAAB" → 2
A never loses the lead
"AAABAB" → 0
Taking the first lead is not a change
"BBB" → 0
The comeback
"AAAABBBBBB" → 1
Lots of changes
"ABBAABBAAB" → 4
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def lead_changes(points):
passJavaScript
function leadChanges(points) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.