Skip to content

Commit

Permalink
bit manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed May 3, 2022
1 parent 2106c2c commit 11d4ac9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 17_Bit-Manipulation/08. Find Kth Bit in Nth Binary String.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/


'''
# Brute Force Solution
class Solution:
def findKthBit(self, n: int, k: int) -> str:
def revinv(s):
s = list(s)
for i in range(len(s)):
if s[i] == '0':
s[i] = '1'
else:
s[i] = '0'
s = ''.join(s)
s = s[::-1]
return s
dp = ['0'] * n
dp[0] = '0'
for i in range(n):
dp[i] = dp[i-1] + '1' + revinv(dp[i-1])
return dp[-1][k-1]
# Time: O(n^2)
# Space: (n)
'''


# Optimal Solution:
class Solution:
def findKthBit(self, n, k):
return str(k / (k & -k) >> 1 & 1 ^ k & 1 ^ 1)

0 comments on commit 11d4ac9

Please sign in to comment.