LeetCode Day 17: Find Kth Bit in Nth Binary String

Day 17/30 – LeetCode streak Today’s problem: Find Kth Bit in Nth Binary String (1545). 'S₁ = "0"', and 'Sₙ = Sₙ₋₁ + "1" + reverse(invert(Sₙ₋₁))'. Length of 'Sₙ' is always '2ⁿ - 1', with a '1' sitting exactly in the middle. The structure looks like this: Sₙ = [Sₙ₋₁] + '1' + reverse(invert(Sₙ₋₁)) So for any position 'k': - Let 'mid = 2^(n-1)' (the middle index, 1-based). - If 'k == mid', answer is '1'. - If 'k < mid', it’s in the left half, which is exactly 'Sₙ₋₁' → recurse: 'findKthBit(n-1, k)'. - If 'k > mid', it’s in 'reverse(invert(Sₙ₋₁))'. Mirror it back into 'Sₙ₋₁' at 'mirror = 2*mid - k' (same as '2ⁿ - k'), recursively get that bit from 'Sₙ₋₁', then flip it. Day 17 takeaway: This is one of those problems where drawing the recursive construction once (left + 1 + reversed/inverted left) makes the “mid / mirror / invert” recursion feel natural—and you never have to actually build the string. #leetcode #dsa #java #recursion #consistency

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories