The TransCompilator

Solved LeetCode program · 606 of 1,000

#0833 · Find And Replace in String

Medium Array chunk-13.md

Original English source

Keep a shared source as empty text. Place "abcd" into source. Keep a shared indices holding 2 and 0. Keep a shared sources holding "cd" and "a". Keep a shared targets holding "ffff" and "eee". Keep a shared used holding 0 and 0. Keep a shared ordercount of 2. Keep a shared best of 0. Keep a shared pick of 0. Keep a shared cursor of 0. Keep a shared result as empty text.

To seek from slot (a whole number). If slot is at least ordercount, return.
If item slot of used is 0 and item slot of indices is less than best.
Set best to item slot of indices. Set pick to slot. Seek from slot plus 1.

To render from spot (a whole number) with slot (a whole number). If spot is less than cursor, return. The head is the slice of source from cursor to spot. Place head into result. Set cursor to spot. The pattern is item slot of sources. The patternlast is the last position of pattern. The size is patternlast plus 1. The finish is spot plus size. The piece is the slice of source from spot to finish. If piece matches pattern, Place item slot of targets into result. If piece matches pattern, Set cursor to finish. If piece does not match pattern, Place piece into result. If piece does not match pattern, Set cursor to finish.

The step is 0.
Repeat while step is less than ordercount.
Set best to 1000000. Set pick to -1. Seek from 0. The slot is pick. Set item slot of used to 1. The spot is item slot of indices. Render from spot with slot. Increase step by 1.

The endspot is the last position of source. The tail is the slice of source from cursor to endspot plus 1. Place tail into result. Show result.

Open in Playground Browse all 1,000 problems

Expected stdout

eeebffff