|
| 1 | +<h2><a href="https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/">1545. Find Kth Bit in Nth Binary String</a></h2><h3>Medium</h3><hr><div><p>Given two positive integers <code>n</code> and <code>k</code>, the binary string <code>S<sub>n</sub></code> is formed as follows:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>S<sub>1</sub> = "0"</code></li> |
| 5 | + <li><code>S<sub>i</sub> = S<sub>i - 1</sub> + "1" + reverse(invert(S<sub>i - 1</sub>))</code> for <code>i > 1</code></li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>Where <code>+</code> denotes the concatenation operation, <code>reverse(x)</code> returns the reversed string <code>x</code>, and <code>invert(x)</code> inverts all the bits in <code>x</code> (<code>0</code> changes to <code>1</code> and <code>1</code> changes to <code>0</code>).</p> |
| 9 | + |
| 10 | +<p>For example, the first four strings in the above sequence are:</p> |
| 11 | + |
| 12 | +<ul> |
| 13 | + <li><code>S<sub>1 </sub>= "0"</code></li> |
| 14 | + <li><code>S<sub>2 </sub>= "0<strong>1</strong>1"</code></li> |
| 15 | + <li><code>S<sub>3 </sub>= "011<strong>1</strong>001"</code></li> |
| 16 | + <li><code>S<sub>4</sub> = "0111001<strong>1</strong>0110001"</code></li> |
| 17 | +</ul> |
| 18 | + |
| 19 | +<p>Return <em>the</em> <code>k<sup>th</sup></code> <em>bit</em> <em>in</em> <code>S<sub>n</sub></code>. It is guaranteed that <code>k</code> is valid for the given <code>n</code>.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre><strong>Input:</strong> n = 3, k = 1 |
| 25 | +<strong>Output:</strong> "0" |
| 26 | +<strong>Explanation:</strong> S<sub>3</sub> is "<strong><u>0</u></strong>111001". |
| 27 | +The 1<sup>st</sup> bit is "0". |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong class="example">Example 2:</strong></p> |
| 31 | + |
| 32 | +<pre><strong>Input:</strong> n = 4, k = 11 |
| 33 | +<strong>Output:</strong> "1" |
| 34 | +<strong>Explanation:</strong> S<sub>4</sub> is "0111001101<strong><u>1</u></strong>0001". |
| 35 | +The 11<sup>th</sup> bit is "1". |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= n <= 20</code></li> |
| 43 | + <li><code>1 <= k <= 2<sup>n</sup> - 1</code></li> |
| 44 | +</ul> |
| 45 | +</div> |
0 commit comments