← All problems

2311. Longest Binary Subsequence Less Than or Equal to K

MediumOpen on LeetCodeProblem statement

Problem Statement

2311. Longest Binary Subsequence Less Than or Equal to K

Medium


You are given a binary string s and a positive integer k.

Return the length of the longest subsequence of s that makes up a binary number less than or equal to k.

Note:

 

Example 1:

Input: s = "1001010", k = 5
Output: 5
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.
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
The length of this subsequence is 5, so 5 is returned.

Example 2:

Input: s = "00101001", k = 1
Output: 6
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.
The length of this subsequence is 6, so 6 is returned.

 

Constraints:

Java

Source file
class Solution {
    public int longestSubsequence(String s, int k) {
        int n = s.length(), len = 0, val = 0,
                bits = (int) (Math.log(k) / Math.log(2)) + 1;
        for (int i = n - 1; i >= 0; i--) {
            if (s.charAt(i) == '0')
                len++;
            else if (len < bits) {
                int temp = val + (1 << len);
                if (temp <= k) {
                    val = temp;
                    len++;
                }
            }
        }
        return len;
    }
}