Comprobar si una string binaria tiene un 0 entre 1 o no | Conjunto 1 (enfoque general)

Dada una string de 0 y 1, debemos verificar que la string dada sea válida o no. La string dada es válida cuando no hay ningún cero presente entre 1. Por ejemplo, 1111, 0000111110, 1111000 son strings válidas pero 01010011, 01010, 101 no lo son. 

Aquí se analiza un enfoque para resolver el problema, otro que usa expresiones regulares se proporciona en el Conjunto 2 

C++

// C++ program to check if a string is valid or not.
#include <bits/stdc++.h>
using namespace std;
  
// Function returns 1 when string is valid
// else returns 0
bool checkString(string s)
{
    int len = s.length();
  
    // Find first occurrence of 1 in s[]
    int first = s.size() + 1;
    for (int i = 0; i < len; i++) {
        if (s[i] == '1') {
            first = i;
            break;
        }
    }
  
    // Find last occurrence of 1 in s[]
    int last = 0;
    for (int i = len - 1; i >= 0; i--) {
        if (s[i] == '1') {
            last = i;
            break;
        }
    }
  
    // Check if there is any 0 in range
    for (int i = first; i <= last; i++)
        if (s[i] == '0')
            return false;
  
    return true;
}
  
// Driver code
int main()
{
    string s = "00011111111100000";
    checkString(s) ? cout << "VALID\n" : cout << "NOT VALID\n";
    return 0;
}

Java

// Java program to check if a string is valid or not.
  
class Test {
    // Method returns 1 when string is valid
    // else returns 0
    static boolean checkString(String s)
    {
        int len = s.length();
  
        // Find first occurrence of 1 in s[]
        int first = 0;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == '1') {
                first = i;
                break;
            }
        }
  
        // Find last occurrence of 1 in s[]
        int last = 0;
        for (int i = len - 1; i >= 0; i--) {
            if (s.charAt(i) == '1') {
                last = i;
                break;
            }
        }
  
        // Check if there is any 0 in range
        for (int i = first; i <= last; i++)
            if (s.charAt(i) == '0')
                return false;
  
        return true;
    }
  
    // Driver method
    public static void main(String args[])
    {
        String s = "00011111111100000";
        System.out.println(checkString(s) ? "VALID" : "NOT VALID");
    }
}

Python

# Python3 program to check if 
# a string is valid or not. 
  
# Function returns 1 when 
# string is valid else 
# returns 0 
def checkString(s):
    
    Len = len(s)
      
    # Find first occurrence 
    # of 1 in s[] 
    first = len(s) + 1
  
    for i in range(Len):
        if(s[i] == '1'):
            first = i
            break
  
    # Find last occurrence 
    # of 1 in s[] 
    last = 0
  
    for i in range(Len - 1, 
                   -1, -1):
        if(s[i] == '1'):
            last = i
            break
  
    # Check if there is any 
    # 0 in range 
    for i in range(first, 
                   last + 1):
        if(s[i] == '0'):
            return False
    return True
  
# Driver code 
s = "00011111111100000"
if(checkString(s)):
    print("VALID")
else:
    print("NOT VALID")
  
# This code is contributed by avanitrachhadiya2155

C#

// C# program to check if a 
// string is valid or not.
using System;
  
class GFG { 
      
    // Method returns 1 when string is valid
    // else returns 0
    static bool checkString(String s)
    {
        int len = s.Length;
  
        // Find first occurrence of 1 in s[]
        int first = 0;
        for (int i = 0; i < len; i++) {
            if (s[i] == '1') {
                first = i;
                break;
            }
        }
  
        // Find last occurrence of 1 in s[]
        int last = 0;
        for (int i = len - 1; i >= 0; i--) {
            if (s[i] == '1') {
                last = i;
                break;
            }
        }
  
        // Check if there is any 0 in range
        for (int i = first; i <= last; i++)
            if (s[i] == '0')
                return false;
  
        return true;
    }
  
    // Driver method
    public static void Main()
    {
        string s = "00011111111100000";
        Console.WriteLine(checkString(s) ?
                   "VALID" : "NOT VALID");
    }
}
  
// This code is contributed by Sam007

Javascript

<script>
  
    // JavaScript program to check  
    // if a string is valid or not.
      
    // Method returns 1 when string is valid
    // else returns 0
    function checkString(s)
    {
        let len = s.length;
    
        // Find first occurrence of 1 in s[]
        let first = 0;
        for (let i = 0; i < len; i++) {
            if (s[i] == '1') {
                first = i;
                break;
            }
        }
    
        // Find last occurrence of 1 in s[]
        let last = 0;
        for (let i = len - 1; i >= 0; i--) {
            if (s[i] == '1') {
                last = i;
                break;
            }
        }
    
        // Check if there is any 0 in range
        for (let i = first; i <= last; i++)
            if (s[i] == '0')
                return false;
    
        return true;
    }
      
    let s = "00011111111100000";
    document.write(checkString(s) ?
                      "VALID" : "NOT VALID");
      
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *