-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthBitInNthBinaryString.java
More file actions
49 lines (46 loc) · 1.37 KB
/
KthBitInNthBinaryString.java
File metadata and controls
49 lines (46 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 1545. Find Kth Bit in Nth Binary String
class Solution {
public char findKthBit(int n, int k) {
String s = "0";
for (int i = 2; i <= n; i++) {
s = s + "1" + reverse(invert(s));
}
return s.charAt(k - 1);
}
public String reverse(String s){
StringBuilder reversed = new StringBuilder();
int n = s.length();
for(int i = n-1; i >= 0; i--){
reversed.append(s.charAt(i));
}
return reversed.toString();
}
public String invert(String s){
StringBuilder inverted = new StringBuilder();
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
inverted .append(c == '0' ? '1' : '0');
}
return inverted.toString();
}
}
// More optimal solution using recursion and bit manipulation
// class Solution {
// public char findKthBit(int n, int k) {
// return (char) ('0' + findKthBitRec(n, k));
// }
// private static int findKthBitRec(int n, int k) {
// if (k == 1) {
// return 0;
// }
// if ((k & (k - 1)) == 0) {
// return 1;
// }
// int totalLen = 1 << n;
// if (k * 2 < totalLen - 1) {
// return findKthBitRec(n - 1, k);
// } else {
// return findKthBitRec(n - 1, totalLen - k) ^ 1;
// }
// }
// }