Dangerous String

By | December 10, 2022

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.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.