二进制形式不是给定二进制字符串子序列的最小数
给定一个大小为N的二进制字符串S , 任务是找到最小的非负整数,它不是给定字符串S的二进制形式的子序列。
例子:
Input: S = “0000”
Output:1
Explanation: 1 whose binary representation is “1” is the smallest non-negative integer which is not a subsequence of the given string in its binary form.
Input: S = “10101”
Output: 8
方法:想法是将给定的字符串转换为其十进制表示形式,例如R 。然后在[0, R]范围内迭代以检查每个整数是否作为给定字符串S中二进制形式的子序列存在。如果不是,则中断循环并打印所需的结果。
请按照以下步骤解决问题:
- 将二进制字符串S的十进制数存储在变量R中。
- 将变量ans初始化为R+1以存储所需的结果。
- 使用变量i在[0, R]范围内迭代
- 将给定的整数i转换为其二进制字符串,并将其存储在字符串P中。
- 检查字符串P是否是字符串S的子序列。如果不是,则将ans更新为i并跳出循环。
- 打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if string str1 is a
// subsequence of string str2
bool isSubsequence(string str1, string str2, int m, int n)
{
// Store index of str1
int j = 0;
// Traverse str2 and str1, and compare
// current character of str2 with first
// unmatched char of str1
for (int i = 0; i < n && j < m; i++)
// If matched, move ahead in str1
if (str1[j] == str2[i])
j++;
// If all characters of str1 were
// found in str2
return (j == m);
}
// Function to find the minimum number which
// is not a subsequence of the given binary
// string in its binary form
void findMinimumNumber(string s)
{
// Store the decimal number of string, S
int r = stoi(s, 0, 2);
// Initialize the required result
int ans = r + 1;
// Iterate in the range [0, R]
for (int i = 0; i <= r; i++) {
// Convert integer i to binary string
string p = "";
int j = i;
while (j > 0) {
p += to_string(j % 2);
j = j / 2;
}
int m = p.length();
int n = s.length();
reverse(p.begin(), p.end());
// Check if the string is not a subsequence
if (!isSubsequence(p, s, m, n)) {
// Update ans and break
ans = i;
break;
}
}
// Print the required result
cout << ans;
}
// Driver Code
int main()
{
// Function Call
string s = "10101";
// Function Call
findMinimumNumber(s);
return 0;
}
Python3
# python 3 program for the above approach
# Function to check if string str1 is a
# subsequence of string str2
def isSubsequence(str1, str2, m, n):
# Store index of str1
j = 0
# Traverse str2 and str1, and compare
# current character of str2 with first
# unmatched char of str1
i = 0
while(i < n and j < m):
# If matched, move ahead in str1
if (str1[j] == str2[i]):
j += 1
i += 1
# If all characters of str1 were
# found in str2
return (j == m)
# Function to find the minimum number which
# is not a subsequence of the given binary
# string in its binary form
def findMinimumNumber(s):
# Store the decimal number of string, S
r = int(s,2)
# Initialize the required result
ans = r + 1
# Iterate in the range [0, R]
for i in range(r+1):
# Convert integer i to binary string
p = ""
j = i
while (j > 0):
p += str(j % 2)
j = j // 2
m = len(p)
n = len(s)
p = p[::-1]
# Check if the string is not a subsequence
if (isSubsequence(p, s, m, n) == False):
# Update ans and break
ans = i
break
# Print the required result
print(ans)
# Driver Code
if __name__ == '__main__':
# Function Call
s = "10101"
# Function Call
findMinimumNumber(s)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if string str1 is a
// subsequence of string str2
static bool isSubsequence(string str1, string str2,
int m, int n)
{
// Store index of str1
int j = 0;
// Traverse str2 and str1, and compare
// current character of str2 with first
// unmatched char of str1
for (int i = 0; i < n && j < m; i++)
// If matched, move ahead in str1
if (str1[j] == str2[i])
j++;
// If all characters of str1 were
// found in str2
return (j == m);
}
// Function to find the minimum number which
// is not a subsequence of the given binary
// string in its binary form
static void findMinimumNumber(string s)
{
// Store the decimal number of string, S
int r = Int32.Parse(s);
// Initialize the required result
int ans = r + 1;
// Iterate in the range [0, R]
for (int i = 0; i <= r; i++) {
// Convert integer i to binary string
string p = "";
int j = i;
while (j > 0) {
p += (j % 2).ToString();
j = j / 2;
}
int m = p.Length;
int n = s.Length;
char[] stringArray = p.ToCharArray();
Array.Reverse(stringArray);
p = new string(stringArray);
// Check if the string is not a subsequence
if (!isSubsequence(p, s, m, n)) {
// Update ans and break
ans = i;
break;
}
}
// Print the required result
Console.WriteLine(ans);
}
// Driver Code
public static void Main()
{
// Function Call
string s = "10101";
// Function Call
findMinimumNumber(s);
}
}
// This code is contributed by ukasp.
输出
8
时间复杂度: O(N*R),其中 R 是给定二进制字符串的十进制表示, S
辅助空间: O(N)