Problem · String
Stars and Bars
Learn this problemProblem statement
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
starsAndBars(s: String, startIndex: int[], endIndex: int[]) → int[]
Complete the function starsAndBars in the editor.
starsAndBars has three parameters:
- 1.
String s: a string to evaluate - 2.
int[] startIndex: the starting index for each query - 3.
int[] endIndex: the ending index for each query
Returns
int[]: element i answers the query from startIndex[i] through endIndex[i]
Examples
Example 1
s = "|**|*|*"startIndex = [1, 1]endIndex = [5, 6]return = [2, 3]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].
Example 2
s = "|*|*|"startIndex = [1]endIndex = [3]return = [1]The substring from index = 1 to index = 3 is "|*|". Its two bars enclose one star, so the answer is [1].
Constraints
s is either "*" or "|"More Tiktok problems
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026
- Obstacle Placement QueriesOA · Seen Jul 2026
- Repeated Grouped Digit SumOA · Seen Jul 2026
- Count Cyclic Digit PairsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026