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.
Function Description
Complete the function starsAndBars in the editor.
starsAndBars has three parameters:
- 1.
String s: a string to evaluate - 2.
int startIndex[n]: the starting indices - 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].
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.
s is either "*" or "|"