xxxxxxxxxx
#include <iostream>
#include <string>
using namespace std;
// Function for calculating total substrings
int count_substrings(string s, int n){
int total = 0;
// Outer Loop for starting point of substring
for(int i = 0; i < n; i++){
// Variables for counting 1s and 0s
int count_ones = 0;
int count_zeros = 0;
// Inner Loop for ending point of substring
for(int j = i; j < n; j++){
// Incrementing if the current character is 1 or 0
if(s[j]=='1'){
++count_ones;
}
else{
++count_zeros;
}
// Checking count of 1s > count of 0s
if(count_ones>count_zeros){
++total;
}
}
}
return total;
}
int main() {
// Length of the given string
int n = 5;
// Given string
string s="11010";
// Calling the count_strings function
cout<<count_substrings(s,n);
}
xxxxxxxxxx
static List<string> AllSubString(string s)
{
var list = new List<string>();
for (int i = 0; i < s.Length; i++)
{
for (int j = 1; j <= s.Length - i; j++)
{
var sub = s.Substring(i, j);
list.Add(sub);
}
}
return list;
}