Skip to the code
cf.
+25 XP

#46 Longest Balanced Reaction Run

medium

A live stream logs viewer reactions in order as "up" or "down" in reactions.

Write longestBalancedRun(reactions) that returns the length of the longest run of back-to-back reactions with exactly as many "up" as "down". Return 0 if there is none.

Example 1

Input: reactions = ["up","down","down","up","up","up","down"]
Output: 6

Indexes 1 to 6 hold 3 ups and 3 downs.

Example 2

Input: reactions = ["up","up"]
Output: 0

There are no downs, so no run balances.

Constraints

  • 0 <= reactions.length <= 10^5
  • Each item is "up" or "down".
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).