1545. Find Kth Bit in Nth Binary String
Difficulty: Medium
Topics: String
, Recursion
, Simulation
Given two positive integers n
and k
, the binary string Sn
is formed as follows:
S1 = "0"
-
Si = Si - 1 + "1" + reverse(invert(Si - 1))
fori > 1
Where +
denotes the concatenation operation, reverse(x)
returns the reversed string x
, and invert(x)
inverts all the bits in x
(0
changes to 1
and 1
changes to 0
).
For example, the first four strings in the above sequence are:
S1 = "0"
S2 = "011"
S3 = "0111001"
S4 = "011100110110001"
Return the kth
bit in Sn
. It is guaranteed that k
is valid for the given n
.
Example 1:
- Input: n = 3, k = 1
- Output: "0"
- Explanation: S3 is "0111001".\ The 1st bit is "0".
Example 2:
- Input: n = 4, k = 11
- Output: "1"
- Explanation: S4 is "011100110110001". The 11th bit is "1".
Example 3:
- Input: n = 3, k = 2
- Output: "0"
Constraints:
1 <= n <= 20
1 <= k <= 2n - 1
Hint:
- Since n is small, we can simply simulate the process of constructing S1 to Sn.
Solution:
We need to understand the recursive process used to generate each binary string Sn
and use this to determine the k
-th bit without constructing the entire string.
Approach:
-
Recursive String Construction:
- S1 = "0".
- For
i > 1
:-
Si
is constructed as:Si = Si-1 + "1" + reverse(invert(Si-1))
-
- This means Si consists of three parts:
- Si-1 (the original part)
- "1" (the middle bit)
- reverse(invert(Si-1)) (the transformed part)
-
Key Observations:
- Si has a length of 2i-1.
- The middle bit (position 2i-1 of Si is always "1".
- If k lies in the first half, it falls within Si-1.
- If k is exactly the middle position, the answer is "1".
- If k is in the second half, it corresponds to the reverse-inverted part.
-
Inverting and Reversing:
- To determine the bit in the second half, map k to its corresponding position in the first half using:
k' = 2i - k
- The bit at this position in the original half is inverted, so we need to flip the result.
- To determine the bit in the second half, map k to its corresponding position in the first half using:
-
Recursive Solution:
- Recursively determine the k-th bit by reducing n and mapping k until n = 1.
Let's implement this solution in PHP: 1545. Find Kth Bit in Nth Binary String
<?php
/**
* @param Integer $n
* @param Integer $k
* @return String
*/
function findKthBit($n, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
?>
Explanation:
- Base Case: If n = 1, Si is "0", so the only possible value for any k is "0".
-
Recursive Steps:
- Calculate the middle index mid which is 2n-1.
- If k matches the middle index, return "1".
- If k is less than mid, the k-th bit lies in the first half, so we recursively find the k-th bit in Sn-1.
- If k is greater than mid, we find the corresponding bit in the reverse-inverted second half and flip it.
Complexity Analysis:
- Time Complexity: O(n) because each recursive call reduces n by 1.
- Space Complexity: O(n) for the recursive call stack.
Example Walkthrough:
-
Input: n = 3 , k = 1
- S3 = "0111001"
- k = 1 falls in the first half, so we look for k = 1 in S2.
- In S2 = "011" , k = 1 corresponds to "0".
-
Input: n = 4 , k = 11
- S4 = "011100110110001"
- The middle index for S4 is 8.
- k = 11 falls in the second half.
- Map k = 11 to the corresponding bit in the first half: k' = 24 - 11 = 5.
- Find k = 5 in S3 , which is "0", then flip it to "1".
By leveraging recursion and properties of the string construction, this solution avoids generating the entire string, making it efficient even for larger n.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)