Algorithm
Find Kth Bit in Nth Binary String
Solution
class Solution:
def findKthBit(self, n: int, k: int) -> str:
# Base case: for S1, return '0'
if n == 1:
return "0"
# Calculate the length of Sn
length = 1 << n # Equivalent to 2^n
# If k is in the first half of the string, recurse with n-1
if k < length // 2:
return self.findKthBit(n - 1, k)
# If k is exactly in the middle, return '1'
elif k == length // 2:
return "1"
# If k is in the second half of the string
else:
# Find the corresponding bit in the first half and invert it
corresponding_bit = self.findKthBit(n - 1, length - k)
return "1" if corresponding_bit == "0" else "0"