Count Substrings[GeeksforGeeks solution]
Question: Given a binary string, count number of substrings that start and end with 1. For example, if the input string is “00100101”, then there are three substrings “1001”, “100101” and “101”.
Input:
The first line contains T denoting the number of testcases. Then follows description of testcases.
Each case contains a string containing 0’s and 1’s.
Output:
For each test case, output a single line denoting number of substrings possible.
Constraints:
1<=T<=100
1<=Lenght of String<=100
Example: Input: 1 10101 Output: 3
CODE:
#include <iostream>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
//code int t;
cin>> t;
while(t--)
{
int ans =0;
int count=0;
string s;
cin>>s;
int n = s.length();
for(int i=0;i<n;i++)
{
if(s[i] == '1')
{
count++;
}
}
ans = (count * (count-1))/2;
cout<<ans<<endl;
}
return 0;
}
FOR IDE:
https://ide.geeksforgeeks.org/HFIXGHuLP6
EASY?

Leave a comment