Problem · String
Bit at an Index After Repeated Binary Expansion
Learn this problemProblem statement
Start with a binary string bits. In one expansion round, replace every character independently:
0becomes00.1becomes10.
After exactly rounds expansions, return the bit at the zero-based position index. The position is guaranteed to exist in the expanded string.
Function
expandedBit(bits: String, rounds: int, index: int) → intExamples
Example 1
bits = "01"rounds = 1index = 2return = 1One expansion produces 0010, whose zero-based index 2 contains 1.
Example 2
bits = "1"rounds = 2index = 3return = 0The two expansions are 1 -> 10 -> 1000, and its last bit is 0.
Example 3
bits = "101"rounds = 0index = 2return = 1With zero rounds, query the original string directly.
Constraints
1 <= bits.length <= 10^5bitscontains only0and1.0 <= rounds <= 300 <= index <= 10^9index < bits.length * 2^rounds.