📌  相关文章
📜  检查给定二进制字符串中子字符串是否存在0到N的二进制表示形式

📅  最后修改于: 2021-04-29 14:31:25             🧑  作者: Mango

给定二进制字符串str和一个整数N,任务是检查字符串的子字符串包含小于或等于给定整数N的所有非负整数的二进制表示形式。

例子:

方法:
使用BitSet和HashMap可以解决上述问题。请按照以下步骤解决问题

  • 初始化map []以标记字符串,并使用位变量ans将数字从十进制转换为二进制。
  • 再将一个变量计数设为零。
  • 使用变量iN1运行循环,并检查是否在映射中标记了相应的数字。
  • 如果没有在map []中标记数字i,则使用变量ans将当前数字转换为二进制。
  • 然后检查转换后的二进制字符串是否为给定字符串的字符串。
  • 如果它不是子字符串,那么
  • 除非没有标记并且二进制数变为零,否则运行while循环
    • 在地图上标记i
    • 增加计数
    • 对转换后的数字进行右移。这样做是因为如果将任何字符串x转换为二进制(例如111001 )并且该子字符串已经在map中标记,则11100将已经被自动标记
      这是基于以下事实:如果i存在, i >> 1也存在。
  • 最后检查是否算? N +1 ,然后打印True
    其他打印False

下面是上述方法的实现:

C++
// C++ program for the above approach 
  
#include  
using namespace std; 
  
// Function to convert decimal to binary 
// representation 
string decimalToBinary(int N) 
{ 
  
    string ans = ""; 
  
    // Iterate over all bits of N 
    while (N > 0) { 
  
        // If bit is 1 
        if (N & 1) { 
            ans = '1' + ans; 
        } 
        else { 
            ans = '0' + ans; 
        } 
  
        N /= 2; 
    } 
  
    // Return binary representation 
    return ans; 
} 
  
// Function to check if binary conversion 
// of numbers from N to 1 exists in the 
// string as a substring or not 
string checkBinaryString(string& str, int N) 
{ 
  
    // To store the count of number 
    // exists as a substring 
    int map[N + 10], cnt = 0; 
  
    memset(map, 0, sizeof(map)); 
  
    // Traverse from N to 1 
    for (int i = N; i > 0; i--) { 
  
        // If current number is not 
        // present in map 
        if (!map[i]) { 
  
            // Store current number 
            int t = i; 
  
            // Find binary of t 
            string s = decimalToBinary(t); 
  
            // If the string s is a 
            // substring of str 
            if (str.find(s) != str.npos) { 
  
                while (t && !map[t]) { 
  
                    // Mark t as true 
                    map[t] = 1; 
  
                    // Increment the count 
                    cnt++; 
  
                    // Update for t/2 
                    t >>= 1; 
                } 
            } 
        } 
    } 
  
    // Special judgment '0' 
    for (int i = 0; i < str.length(); i++) { 
        if (str[i] == '0') { 
            cnt++; 
            break; 
        } 
    } 
    // If the count is N+1, return "yes" 
    if (cnt == N + 1) 
        return "True"; 
    else
        return "False"; 
} 
  
// Driver Code 
int main() 
{ 
    // Given String 
    string str = "0110"; 
  
    // Given Number 
    int N = 3; 
  
    // Function Call 
    cout << checkBinaryString(str, N); 
    return 0; 
}


Java
// Java program for the above approach 
import java.util.*;
  
class GFG{ 
  
// Function to convert decimal to binary 
// representation 
static String decimalToBinary(int N) 
{ 
    String ans = ""; 
  
    // Iterate over all bits of N 
    while (N > 0) 
    { 
          
        // If bit is 1 
        if (N % 2 == 1)
        { 
            ans = '1' + ans; 
        } 
        else
        { 
            ans = '0' + ans; 
        } 
        N /= 2; 
    } 
  
    // Return binary representation 
    return ans; 
} 
  
// Function to check if binary conversion 
// of numbers from N to 1 exists in the 
// String as a subString or not 
static String checkBinaryString(String str, int N) 
{ 
      
    // To store the count of number 
    // exists as a subString 
    int []map = new int[N + 10];
    int cnt = 0; 
  
    // Traverse from N to 1 
    for(int i = N; i > 0; i--)
    { 
  
        // If current number is not 
        // present in map 
        if (map[i] == 0)
        { 
              
            // Store current number 
            int t = i; 
  
            // Find binary of t 
            String s = decimalToBinary(t); 
  
            // If the String s is a 
            // subString of str 
            if (str.contains(s))
            { 
                while (t > 0 && map[t] == 0)
                { 
                      
                    // Mark t as true 
                    map[t] = 1; 
  
                    // Increment the count 
                    cnt++; 
  
                    // Update for t/2 
                    t >>= 1; 
                } 
            } 
        } 
    } 
  
    // Special judgment '0' 
    for(int i = 0; i < str.length(); i++)
    { 
        if (str.charAt(i) == '0')
        { 
            cnt++; 
            break; 
        } 
    }
      
    // If the count is N+1, return "yes" 
    if (cnt == N + 1) 
        return "True"; 
    else
        return "False"; 
} 
  
// Driver Code 
public static void main(String[] args) 
{ 
      
    // Given String 
    String str = "0110"; 
  
    // Given number 
    int N = 3; 
  
    // Function call 
    System.out.print(checkBinaryString(str, N)); 
}
} 
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of
# the above approach
  
# Function to convert decimal to 
# binary representation
def decimalToBinary(N):
  
    ans = ""
  
    # Iterate over all bits of N
    while(N > 0):
  
        # If bit is 1
        if(N & 1):
            ans = '1' + ans
        else:
            ans = '0' + ans
  
        N //= 2
  
    # Return binary representation
    return ans
  
# Function to check if binary conversion
# of numbers from N to 1 exists in the
# string as a substring or not
def checkBinaryString(str, N):
  
    # To store the count of number
    # exists as a substring
    map = [0] * (N + 10)
    cnt = 0
  
    # Traverse from N to 1
    for i in range(N, -1, -1):
  
        # If current number is not
        # present in map
        if(not map[i]):
  
            # Store current number
            t = i
  
            # Find binary of t
            s = decimalToBinary(t)
  
            # If the string s is a
            # substring of str
            if(s in str):
                while(t and not map[t]):
                      
                    # Mark t as true
                    map[t] = 1
  
                    # Increment the count
                    cnt += 1
  
                    # Update for t/2
                    t >>= 1
  
    # Special judgment '0'
    for i in range(len(str)):
        if(str[i] == '0'):
            cnt += 1
            break
  
    # If the count is N+1, return "yes" 
    if(cnt == N + 1):
        return "True"
    else:
        return "False"
  
# Driver Code
if __name__ == '__main__':
  
    # Given String
    str = "0110"
  
    # Given Number
    N = 3
  
    # Function Call
    print(checkBinaryString(str, N))
  
# This code is contributed by Shivam Singh


C#
// C# program for the above approach 
using System;
  
class GFG{ 
  
// Function to convert decimal to binary 
// representation 
static String decimalToBinary(int N) 
{ 
    String ans = ""; 
  
    // Iterate over all bits of N 
    while (N > 0) 
    { 
          
        // If bit is 1 
        if (N % 2 == 1)
        { 
            ans = '1' + ans; 
        } 
        else
        { 
            ans = '0' + ans; 
        } 
        N /= 2; 
    } 
  
    // Return binary representation 
    return ans; 
} 
  
// Function to check if binary conversion 
// of numbers from N to 1 exists in the 
// String as a subString or not 
static String checkBinaryString(String str, int N) 
{ 
      
    // To store the count of number 
    // exists as a subString 
    int []map = new int[N + 10];
    int cnt = 0; 
  
    // Traverse from N to 1 
    for(int i = N; i > 0; i--)
    { 
  
        // If current number is not 
        // present in map 
        if (map[i] == 0)
        { 
              
            // Store current number 
            int t = i; 
  
            // Find binary of t 
            String s = decimalToBinary(t); 
  
            // If the String s is a 
            // subString of str 
            if (str.Contains(s))
            { 
                while (t > 0 && map[t] == 0)
                { 
                      
                    // Mark t as true 
                    map[t] = 1; 
  
                    // Increment the count 
                    cnt++; 
  
                    // Update for t/2 
                    t >>= 1; 
                } 
            } 
        } 
    } 
  
    // Special judgment '0' 
    for(int i = 0; i < str.Length; i++)
    { 
        if (str[i] == '0')
        { 
            cnt++; 
            break; 
        } 
    }
      
    // If the count is N+1, return "yes" 
    if (cnt == N + 1) 
        return "True"; 
    else
        return "False"; 
} 
  
// Driver Code 
public static void Main(String[] args) 
{ 
      
    // Given String 
    String str = "0110"; 
  
    // Given number 
    int N = 3; 
  
    // Function call 
    Console.Write(checkBinaryString(str, N)); 
}
} 
  
// This code is contributed by PrinciRaj1992


输出:
True