The TransCompilator

Solved LeetCode program · 896 of 1,000

#1234 · Replace the Substring for Balanced String

Medium String chunk-18.md

Original English source

The text is "QQQW". Keep a shared total holding 0, 0, 0, and 0. Keep a shared window holding 0, 0, 0, and 0. Keep a shared target of 0. Keep a shared fit of 0.

To Check. Set fit to 1.
For every kind from 0 to 3.
The outside is item kind of total minus item kind of window. If outside is more than target, set fit to 0.

The size is 0.
For every spot from 0 to the last position of text.
Add 1 to size.

Set target to size divided by 4.
For every spot from 0 to the last position of text.
The symbol is character spot of text. The kind is 0. If symbol is "W", set kind to 1. If symbol is "E", set kind to 2. If symbol is "R", set kind to 3. Increase item kind of total by 1.

The best is size. The left is 0. The right is 0. Check. If fit is 1, set best to 0.
Repeat while right is less than size.
Add 1 to right. The at is right minus 1. The symbol is character at of text. The kind is 0. If symbol is "W", set kind to 1. If symbol is "E", set kind to 2. If symbol is "R", set kind to 3. Increase item kind of window by 1. Check.
Repeat while fit is 1 and left is less than right.
The span is right minus left. If span is less than best, set best to span. The old is character left of text. The oldkind is 0. If old is "W", set oldkind to 1. If old is "E", set oldkind to 2. If old is "R", set oldkind to 3. Decrease item oldkind of window by 1. Add 1 to left. Check.

Show best.

Open in Playground Browse all 1,000 problems

Expected stdout

2