Longest Winning Streak

EasySliding Window≈ 20 min

A team's season is written as a string games of W (a win) and L (a loss), one letter per game, in the order they were played. The league allows the team to replay one lost game, and the team would win the replay.

Return the length of the longest winning streak the team could have after replaying at most one loss: the most games in a row that would all be wins.

Examples

text
games = "WWLWWWLW"   →  6
games = "LLL"        →  1
games = "WWWW"       →  4
games = "WLWLW"      →  3

In the first example, replaying the first loss turns WWLWWW into six wins in a row. In the second, one replay gives a streak of one.

Constraints

  • Between 0 and 20,000 games, each W or L.
  • Aim for O(n): look at each game a constant number of times.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.