FastPrepApply Binary State Operations

Apply Binary State Operations

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

Problem statement

For this exercise, assume the initial binary state is supplied as a string state. Apply the strings in operations from left to right:

  • "L": find the smallest index whose current value is 0 and change it to 1. If no zero remains, do nothing.
  • "Cj": change the value at zero-based index j to 0, regardless of its current value. The index may contain more than one digit.

Return the final binary state as a string.

Function

applyBinaryStateOperations(state: String, operations: String[]) → String

Examples

Example 1

state = "00100"operations = ["L","C2","L"]return = "11000"

The first load sets index 0, producing 10100. Clearing index 2 produces 10000. The final load sets the new leftmost zero at index 1.

Example 2

state = "111"operations = ["L","C1","L"]return = "111"

The first load has no zero to change. Clearing index 1 creates the state 101, and the final load sets that position back to 1.

Constraints

  • 1 <= state.length <= 100000
  • state contains only 0 and 1.
  • 1 <= operations.length <= 100000
  • Every operation is either "L" or "Cj" for a valid index 0 <= j < state.length.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String applyBinaryStateOperations(String state, String[] operations) {
    // Write your code here.
}
state"00100"
operations["L","C2","L"]
expected"11000"
Checking account…