Dangerous String

Last Updated :

A string contains ‘0’ and ‘1’.You have to find out whether the string is dangerous or not. The string will be dangerous in the following two conditions.

  1. There are at least 7 continuous occurrences of ‘1’.
  2. There are at least 7 continuous occurrences of ‘0’.

Examples:

Input : 1000000001
Output : YES
Explanation: It has more than 7 continuous occurrences of '0'.

Input :  001001
Output :  NO
Explanation:String length is less the 7.

Approach: Follow the following steps:

  1. Find the length of the string, If the length of the string is less than 7 then print “NO” because there is no chance of 7 continuous ‘0’ or ‘1’.
  2. If the length of the string is exactly 7 then if anyone in ‘0’ or ‘1’ has the frequency 7 then print YES.
  3. If the string length is greater than 7 then iterate the complete string to find 7 continuous ‘1’ and ‘0’.

Below is the implementation of the above approach.

// CPP program to find the string is
// Dangerous or not
// #include <bits/stdc++.h > 
#include <iostream>
using namespace std;

// Function to find string
// Dangerous or not
void Dangerous(string str)
{
    int freq[2] = { 0 }, flag = 0;
    
    // If string length less than 7
    if (str.length() < 7) 
    {
        cout << "NO";
    }

    // If string length is equal to 7
    if (str.length() == 7) {
        for (int i = 0; i < 7; i++) 
        {
            freq[str[i] - '0']++;
        }

        if (freq[0] == 7 or freq[1] == 7)
            cout << "YES";
        else
            cout << "NO";
    }

    // If string length is greater then 7
    else if (str.length() > 7) {
        for (int i = 0; i < str.length() - 6; i++) {
            if (str[i] == '0' and str[i + 1] == '0' 
            and str[i + 2] == '0' and str[i + 3] == '0' 
            and str[i + 4] == '0' and str[i + 5] == '0' 
            and str[i + 6] == '0') 
            {
                cout << "YES";
                flag = 1;
                break;
            }

            if (str[i] == '1' and str[i + 1] == '1' 
            and str[i + 2] == '1' and str[i + 3] == '1' 
            and str[i + 4] == '1' and str[i + 5] == '1' 
            and str[i + 6] == '1') 
            {
                cout << "YES";
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            cout << "NO";
    }
}

// Driver Program.
int main()
{
    string str = "1000000001";
    Dangerous(str);
    return 0;
}
Output:
YES

Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 10 Dec, 2022 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read
  • Check if a Number is Hexacontagon

    Given an integer N, the task is to check if it is a hexacontagon number or not. Hexacontagon number is class of figurate number. It has 60 –…

    2 min read