package leetcode;

/**
 * https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/
 */
public class Problem1545 {
    public char findKthBit(int n, int k) {
        String s = "0";
        for (int i = 0; i < n - 1; i++) {
            s = s + "1" + reverse(inverse(s));
            if (s.length() >= k) {
                return s.charAt(k - 1);
            }
        }
        return '0';
    }

    private static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    private static String inverse(String s) {
        String str = "";
        for (char c : s.toCharArray()) {
            str += c == '0' ? '1' : '0';
        }
        return str;
    }

    public static void main(String[] args) {
        Problem1545 prob = new Problem1545();
        System.out.println(prob.findKthBit(3, 1)); // "0"
        System.out.println(prob.findKthBit(4, 11)); // "1"
        System.out.println(prob.findKthBit(1, 1)); // "0"
        System.out.println(prob.findKthBit(2, 3)); // "1"
        System.out.println(prob.findKthBit(20, 1048575)); // "1"
    }
}

@@ -572,17 +572,40 @@
 ring
- str = %22%22
+Builder sb = new StringBuilder()
 ;%0A  
@@ -660,14 +660,17 @@
    s
-tr += 
+b.append(
 c ==
@@ -681,24 +681,25 @@
  ? '1' : '0'
+)
 ;%0A        %7D%0A
@@ -718,407 +718,21 @@
 rn s
-tr;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1545 prob = new Problem1545();%0A        System.out.println(prob.findKthBit(3, 1)); // %220%22%0A        System.out.println(prob.findKthBit(4, 11)); // %221%22%0A        System.out.println(prob.findKthBit(1, 1)); // %220%22%0A        System.out.println(prob.findKthBit(2, 3)); // %221%22%0A        System.out.println(prob.findKthBit(20, 1048575)); // %221%22
+b.toString();
 %0A   

Solve problem 1545