Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
We build a table of n
rows (1-indexed). We start by writing 0
in the 1st
row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0
with 01
, and each occurrence of 1
with 10
.
n = 3
, the 1st
row is 0
, the 2nd
row is 01
, and the 3rd
row is 0110
.Given two integer n
and k
, return the kth
(1-indexed) symbol in the nth
row of a table of n
rows.
Example 1:
Input: n = 1, k = 1
Output: 0
Explanation: row 1: 0
Example 2:
Input: n = 2, k = 1
Output: 0
Explanation:
row 1: 0
row 2: 01
Example 3:
Input: n = 2, k = 2
Output: 1
Explanation:
row 1: 0
row 2: 01
Constraints:
1 <= n <= 30
1 <= k <= 2n - 1
public int kthGrammar(int N, int K) {
return Integer.bitCount(K - 1) & 1;
}
int kthGrammar(int N, int K) {
int n;
for (n = 0, K -= 1; K ; K &= (K - 1)) n++;
return n & 1;
}
def kthGrammar(self, N, K):
return bin(K - 1).count('1') & 1
In our experience, we suggest you solve this K-th Symbol in Grammar LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the K-th Symbol in Grammar LeetCode Solution
I hope this K-th Symbol in Grammar LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>