📌  相关文章
📜  检查给定的二进制字符串遵循然后给定的条件

📅  最后修改于: 2021-04-17 08:25:22             🧑  作者: Mango

给定一个二进制字符串str ,任务是检查给定的字符串是否符合以下条件:

  • 字符串以‘1’开头。
  • 每个“ 1”后跟一个空字符串( “” ), “ 1”“ 00”
  • 每个“ 00”后跟一个空字符串( “” ), “ 1”

如果给定的字符串符合上述条件,则打印“有效字符串”,否则打印“无效字符串”
例子:

方法:想法是使用递归。以下是这些步骤:

  1. 检查0字符是否为“ 1” 。如果它不是‘1’ ,则返回false,因为该字符串不符合条件1。
  2. 要检查字符串满足所述第二条件时,使用递归SUBSTR从第一索引开始要求字符串中C()函数++。
  3. 要检查满足第三个条件的字符串,首先,我们需要检查字符串长度是否大于2。如果是,则检查在第一个索引和第二个索引处是否存在“ 0” 。如果是,则递归调用从第三个索引开始的字符串。
  4. 在任何递归调用中,如果字符串为空,则我们遍历了满足所有给定条件的完整字符串,并打印了“ Valid String”
  5. 在任何递归调用中,如果给定条件不满足,则停止该递归并打印“ Invalid String”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to check if the
// string follows rules or not
bool checkrules(string s)
{
    if (s.length() == 0)
        return true;
  
    // Check for the first condition
    if (s[0] != '1')
        return false;
  
    // Check for the third condition
    if (s.length() > 2) {
        if (s[1] == '0' && s[2] == '0')
            return checkrules(s.substr(3));
    }
  
    // Check for the second condition
    return checkrules(s.substr(1));
}
  
// Driver Code
int main()
{
    // Given String str
    string str = "1111";
  
    // Function Call
    if (checkrules(str)) {
        cout << "Valid String";
    }
    else {
        cout << "Invalid String";
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to check if the
// String follows rules or not
static boolean checkrules(String s)
{
    if (s.length() == 0)
        return true;
  
    // Check for the first condition
    if (s.charAt(0) != '1')
        return false;
  
    // Check for the third condition
    if (s.length() > 2)
    {
        if (s.charAt(1) == '0' && 
            s.charAt(2) == '0')
            return checkrules(s.substring(3));
    }
  
    // Check for the second condition
    return checkrules(s.substring(1));
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given String str
    String str = "1111";
  
    // Function call
    if (checkrules(str)) 
    {
        System.out.print("Valid String");
    }
    else
    {
        System.out.print("Invalid String");
    }
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for the above approach 
  
# Function to check if the 
# string follows rules or not
def checkrules(s):
      
    if len(s) == 0:
        return True
          
    # Check for the first condition
    if s[0] != '1':
        return False
          
    # Check for the third condition
    if len(s) > 2:
        if s[1] == '0' and s[2] == '0':
            return checkrules(s[3:])
              
    # Check for the second condition
    return checkrules(s[1:])
      
# Driver code
if __name__ == '__main__':
      
    # Given string
    s = '1111'
      
    # Function call
    if checkrules(s):
        print('valid string')
    else:
        print('invalid string')
          
# This code is contributed by virusbuddah_


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if the
// String follows rules or not
static bool checkrules(String s)
{
    if (s.Length == 0)
        return true;
  
    // Check for the first condition
    if (s[0] != '1')
        return false;
  
    // Check for the third condition
    if (s.Length > 2)
    {
        if (s[1] == '0' && 
            s[2] == '0')
            return checkrules(s.Substring(3));
    }
  
    // Check for the second condition
    return checkrules(s.Substring(1));
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given String str
    String str = "1111";
  
    // Function call
    if (checkrules(str)) 
    {
        Console.Write("Valid String");
    }
    else
    {
        Console.Write("Invalid String");
    }
}
}
  
// This code is contributed by PrinciRaj1992


输出:
Valid String

时间复杂度: O(N) ,其中N是字符串的长度。
辅助空间: O(1)