Problem Β· Prefix Sum

Binary String Requests

● MediumDatabricksINTERNOA

You are given a string binaryString consisting of '0's and '1's, as well as an array of strings requests containing requests of two types:

  • requests[i] = "count:<index>" - find the number of '0's in binaryString before and including the specified 0-based index.
  • requests[i] = "flip" - flip all elements of binaryString, i.e. change every '0' to '1', and every '1' to '0'.
  • Return an array answers, where answers[i] contains the answer for the respective count request.

    πŸ‘β‹†ο½‘Λšβ‹†β€ Credit to πŸšπŸ«§π“‡Ό Charlotte Λ–Β°

    Examples
    01 Β· Example 1
    binaryString = "1111010"
    requests = ["count:4", "count:6", "flip", "count:4", "flip", "count:2"]
    return = [1, 2, 4, 0]
    Explnanation is an educated guess. If you find anythign wrong, pls feel free let me know! Manyyy thanks in advance!! 🦘

    For binaryString = "1111010" and requests = ["count:4", "count:6", "flip", "count:4", "flip", "count:2"], the output should be solution(binaryString, requests) = [1, 2, 4, 0].

    • After the first request "count:4", the number of '0's before and including index 4 is 1.
    • After the second request "count:6", the number of '0's before and including index 6 is 2.
    • After the "flip" request, the binaryString becomes "0000101".
    • After the next "count:4" request, the number of '0's before and including index 4 is 4.
    • Another "flip" request changes the binaryString back to "1111010".
    • Finally, after the "count:2" request, the number of '0's before and including index 2 is 0.
    Constraints
    🫎🫎
    More Databricks problems
    drafts saved locally
    public int[] databricksStringRequests(String binaryString, String[] requests) {
      // write your code here
    }
    
    binaryString"1111010"
    requests["count:4", "count:6", "flip", "count:4", "flip", "count:2"]
    expected[1, 2, 4, 0]
    sign in to submit