给定二进制字符串str ,任务是检查给定的字符串是否符合以下条件:
- 字符串以‘1’开头。
- 每个‘1’后跟空字符串( “” )、 ‘1’或“00” 。
- 每个“00”后跟空字符串( “” ), ‘1’ 。
如果给定的字符串符合上述条件,则打印“有效字符串”,否则打印“无效字符串” 。
例子:
Input: str = “1000”
Output: False
Explanation:
The given string starts with “1” and has “00” followed by the “1” which is not the given criteria.
Hence, the given string is “Invalid String”.
Input: str = “1111”
Output: True
Explanation:
The given string starts with 1 and has 1 followed by all the 1’s.
Hence, the given string is “Valid String”.
方法:想法是使用递归。以下是步骤:
- 检查第0个字符是否为“1” 。如果它不是‘1’ ,则返回 false,因为该字符串不符合条件 1。
- 要检查满足第二个条件的字符串,请使用 C++ 中的 substr()函数从第一个索引开始递归调用字符串。
- 要检查满足第三个条件的字符串,首先需要检查字符串长度是否大于 2。如果是,则检查第一个和第二个索引处是否存在“0” 。如果是,则递归调用从第 3 个索引开始的字符串。
- 在任何递归调用中,如果字符串为空,那么我们已经遍历了满足所有给定条件的完整字符串并打印“有效字符串” 。
- 在任何递归调用中,如果给定条件不满足,则停止该递归并打印“无效字符串” 。
下面是上述方法的实现:
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
Javascript
输出:
Valid String
时间复杂度: O(N) ,其中 N 是字符串的长度。
辅助空间: O(1) 。