| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.FindKthBitInNthBinaryString; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class FindKthBitInNthBinaryStringBruteForce : IFindKthBitInNthBinaryString |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(2^n) |
| | 21 | | /// Space complexity - O(2^n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="n"></param> |
| | 24 | | /// <param name="k"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public char FindKthBit(int n, int k) |
| 2 | 27 | | { |
| 2 | 28 | | var s = "0"; |
| | 29 | |
|
| 14 | 30 | | for (var i = 0; i < n - 1; i++) |
| 5 | 31 | | { |
| 5 | 32 | | s = ModifyString(s); |
| 5 | 33 | | } |
| | 34 | |
|
| 2 | 35 | | return s[k - 1]; |
| 2 | 36 | | } |
| | 37 | |
|
| | 38 | | private static string ModifyString(string s) |
| 5 | 39 | | { |
| 5 | 40 | | var stringBuilder = new StringBuilder(); |
| | 41 | |
|
| 5 | 42 | | stringBuilder.Append(s); |
| 5 | 43 | | stringBuilder.Append('1'); |
| | 44 | |
|
| 40 | 45 | | for (var i = s.Length - 1; i >= 0; i--) |
| 15 | 46 | | { |
| 15 | 47 | | stringBuilder.Append(s[i] == '0' ? '1' : '0'); |
| 15 | 48 | | } |
| | 49 | |
|
| 5 | 50 | | return stringBuilder.ToString(); |
| 5 | 51 | | } |
| | 52 | | } |