You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2311\. Longest Binary Subsequence Less Than or Equal to K
2
+
3
+
Medium
4
+
5
+
You are given a binary string `s` and a positive integer `k`.
6
+
7
+
Return _the length of the **longest** subsequence of_`s`_that makes up a **binary** number less than or equal to_`k`.
8
+
9
+
Note:
10
+
11
+
* The subsequence can contain **leading zeroes**.
12
+
* The empty string is considered to be equal to `0`.
13
+
* A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
14
+
15
+
**Example 1:**
16
+
17
+
**Input:** s = "1001010", k = 5
18
+
19
+
**Output:** 5
20
+
21
+
**Explanation:** The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
22
+
23
+
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
24
+
25
+
The length of this subsequence is 5, so 5 is returned.
26
+
27
+
**Example 2:**
28
+
29
+
**Input:** s = "00101001", k = 1
30
+
31
+
**Output:** 6
32
+
33
+
**Explanation:** "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
34
+
35
+
The length of this subsequence is 6, so 6 is returned.
0 commit comments