FastPrepFastPrep
Problem Brief

Stars and Bars

NEW GRADOA
See Tiktok online assessment and hiring insights

Given a string s consisting of stars "*" and bars "|", an array of starting indices startIndex, and an array of ending indices endIndex, determine the number of stars between any two bars within the substring between the two indices, inclusive. Note that in this problem, indexing starts at 1.

  • A star is represented as an asterisk ("*" = ascii decimal 42)
  • A bar is represented as a pipe ("|" = ascii decimal 124)
  • Function Description

    Complete the function starsAndBars in the editor.

    starsAndBars has three parameters:

    1. 1. String s: a string to evaluate
    2. 2. int startIndex[n]: the starting indices
    3. 3. int endIndex[n]: the ending indices

    Returns

    int[n]: each element[i] answers the query of startIndex[i] to endIndex[i]

    1Example 1

    Input
    s = "|**|*|*", startIndex = [1, 1], endIndex = [5, 6]
    Output
    [2, 3]
    Explanation
    For the first pair of indices, (1, 5), the substring is "|**|*". There are 2 stars between a pair of bars.
    For the second pair of indices, (1, 6), the substring is "|**|*|". There are 2 + 1 = 3 stars between bars.
    Both of the answers are returned in an array, [2, 3].

    2Example 2

    Input
    s = "|*|*|", startIndex = [1], endIndex = [3]
    Output
    0
    Explanation
    The substring from index = 1 to index = 3 is "|*|". There is no consecutive pair of bars in this string.

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 105
  • 1 ≤ startIndex[i] ≤ endIndex[i] ≤ n
  • Each character of s is either "*" or "|"
  • public int[] starsAndBars(String s, int[] startIndex, int[] endIndex) {
        // write your code here
    }
    
    Input

    s

    "|**|*|*"

    startIndex

    [1, 1]

    endIndex

    [5, 6]

    Output

    [2, 3]

    Sign in to submit your solution.