class Solution:
def findKthBit(self, n: int, k: int) -> str:
def helper(n, k):
if n == 1:
return '0'
length = (1 << n) - 1
mid = (length // 2) + 1
if k == mid:
return '1'
elif k < mid:
return helper(n - 1, k)
else:
mirror = length - k + 1
bit = helper(n - 1, mirror)
return '1' if bit == '0' else '0'
return helper(n, k)
class Solution {
public:
char findKthBit(int n, int k) {
if (n == 1) return '0';
int length = (1 << n) - 1;
int mid = (length / 2) + 1;
if (k == mid) return '1';
else if (k < mid) return findKthBit(n - 1, k);
else {
int mirror = length - k + 1;
char bit = findKthBit(n - 1, mirror);
return bit == '0' ? '1' : '0';
}
}
};
class Solution {
public char findKthBit(int n, int k) {
if (n == 1) return '0';
int length = (1 << n) - 1;
int mid = (length / 2) + 1;
if (k == mid) return '1';
else if (k < mid) return findKthBit(n - 1, k);
else {
int mirror = length - k + 1;
char bit = findKthBit(n - 1, mirror);
return bit == '0' ? '1' : '0';
}
}
}
var findKthBit = function(n, k) {
function helper(n, k) {
if (n === 1) return '0';
let length = (1 << n) - 1;
let mid = Math.floor(length / 2) + 1;
if (k === mid) return '1';
else if (k < mid) return helper(n - 1, k);
else {
let mirror = length - k + 1;
let bit = helper(n - 1, mirror);
return bit === '0' ? '1' : '0';
}
}
return helper(n, k);
};
Given two integers n
and k
, you are to find the k
-th bit in the n
-th binary string Sn
constructed as follows:
S1
= "0"i > 1
, Si = Si-1 + "1" + reverse(invert(Si-1))
Here, reverse(x)
means reversing the string x
, and invert(x)
means inverting every bit in x
(changing '0' to '1' and '1' to '0').
You are to return the k
-th character (1-indexed) of Sn
.
Constraints:
n
≤ 20k
≤ 2n - 1
At first glance, it seems we need to build the entire binary string Sn
and simply return the k
-th character. However, since the length of Sn
grows exponentially (for example, S20
is over a million characters long), this brute-force approach is not feasible.
Instead, we need to find a way to determine the k
-th character without generating the whole string. By analyzing the recursive construction of Sn
, we realize that each string is built from the previous one, a '1', and the reversed-inverted previous string. This structure is symmetric, and the position of k
tells us which part of the construction it falls into.
The challenge is to map the k
-th position back to a corresponding position in a smaller problem, potentially inverting the bit if it falls into the reversed-inverted part.
Sn
is built as: Sn-1 + "1" + reverse(invert(Sn-1))
.Sn
is 2^n - 1
.mid = (length // 2) + 1
.n == 1
, return '0' (base case).mid
as described above.k == mid
, return '1' (it's the center).
k < mid
, the answer is the k
-th bit of Sn-1
(left half).
k > mid
, find the "mirrored" position in the left half: mirror = length - k + 1
.
mirror
in Sn-1
.
Let's consider n = 3
, k = 5
:
S1 = "0"
S2 = "0" + "1" + reverse(invert("0")) = "0" + "1" + "1" = "011"
S3 = "011" + "1" + reverse(invert("011"))
S3 = "011" + "1" + "001" = "0111001"
By exploiting the recursive structure and symmetry of the binary string construction, we can efficiently find the k
-th bit without generating huge strings. The key insight is to recursively map the position back to a smaller string and invert when necessary, reducing both time and space complexity from exponential to linear in n
. This makes the solution elegant, efficient, and scalable even for the largest constraints.