First pass: pair every '(' with its ')' using a stack, so the walk can jump between them.
s
Both ends of a pair share a color; ~k names the partner index. Hitting a bracket jumps to its partner and reverses direction.
stack of open '(' indices
output (letters in the order the walker meets them)
filled during the walk