Longest Substring containing ‘1’
Given a binary string, the task is to print the length of the longest substring containing only ‘1’.
Examples:
Input: 110
Output: 2
Explanation: Length of the longest substring containing only ‘1’ is “11”.
Input: 11101110
Output: 3
Approach: Traverse the string and count the number of contiguous 1‘s encountered. Store the maximum number of contiguous 1s in a variable, say max. Finally, print the value of max obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxlength(string s)
{
int n = s.length(), i, j;
int ans = 0;
for (i = 0; i <= n - 1; i++) {
if (s[i] == '1' ) {
int count = 1;
for (j = i + 1; j <= n - 1
&& s[j] == '1' ;
j++)
count++;
ans = max(ans, count);
}
}
return ans;
}
int main()
{
string s = "11101110" ;
cout << maxlength(s) << endl;
return 0;
}
|
Java
class GFG {
static int maxlength(String s)
{
int n = s.length(), i, j;
int ans = 0 ;
for (i = 0 ; i <= n - 1 ; i++) {
if (s.charAt(i) == '1' ) {
int count = 1 ;
for (j = i + 1 ;
j <= n - 1 && s.charAt(j) == '1' ; j++)
count++;
ans = Math.max(ans, count);
}
}
return ans;
}
public static void main(String[] args)
{
String s = "11101110" ;
System.out.println(
"Length of longest substring containing '1' = "
+ maxlength(s));
}
}
|
Python3
def maxlength(s):
n = len (s)
ans = 0 ;
for i in range (n):
if (s[i] = = '1' ):
count = 1
j = i + 1
while (j < = n - 1 and s[j] = = '1' ):
count + = 1
j + = 1
ans = max (ans, count)
return ans
s = "11101110" ;
print (maxlength(s))
|
C#
using System;
class GFG{
static int maxlength(String s)
{
int n = s.Length, i, j;
int ans = 0;
for (i = 0; i <= n - 1; i++)
{
if (s[i] == '1' )
{
int count = 1;
for (j = i + 1;
j <= n - 1 && s[j] == '1' ;
j++)
count++;
ans = Math.Max(ans, count);
}
}
return ans;
}
public static void Main(String[] args)
{
String s = "11101110" ;
Console.Write(maxlength(s));
}
}
|
Javascript
<script>
function maxlength(s)
{
let n = s.length, i, j;
let ans = 0;
for (i = 0; i <= n - 1; i++) {
if (s[i] == '1 ') {
let count = 1;
for (j = i + 1; j <= n - 1
&& s[j] == ' 1';
j++)
count++;
ans = Math.max(ans, count);
}
}
return ans;
}
let s = "11101110" ;
document.write(maxlength(s));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Counting Approach:
Follow the steps to implement the approach:
- Initialize two variables, count and maxCount, to 0.
- Iterate through the string from left to right.
- If the current character is ‘1’, increment count by 1.
- If the current character is ‘0’, update maxCount to the maximum of count and maxCount, and reset count to 0.
- After the iteration, update maxCount to the maximum of count and maxCount again.(if the substring containing only ‘1’ is in the end of the string.)
- The updated maxCount is the length of the longest substring containing only ‘1’.
Below is the implementation:
C++
#include <iostream>
#include <string>
using namespace std;
int longestSubstringLength(string Str) {
int count = 0;
int maxCount = 0;
for ( char c : Str) {
if (c == '1' ) {
count++;
} else {
maxCount = max(count, maxCount);
count = 0;
}
}
maxCount = max(count, maxCount);
return maxCount;
}
int main() {
string Str = "11101110" ;
cout<< longestSubstringLength(Str)<<endl;
return 0;
}
|
Java
public class GFG {
public static int longestSubstringLength(String str)
{
int count = 0 ;
int maxCount = 0 ;
for ( char c : str.toCharArray()) {
if (c == '1' ) {
count++;
}
else {
maxCount = Math.max(count, maxCount);
count = 0 ;
}
}
maxCount = Math.max(count, maxCount);
return maxCount;
}
public static void main(String[] args)
{
String str = "11101110" ;
System.out.println(longestSubstringLength(str));
}
}
|
Python3
def longest_substring_length(s):
count = 0
max_count = 0
for c in s:
if c = = '1' :
count + = 1
else :
max_count = max (count, max_count)
count = 0
max_count = max (count, max_count)
return max_count
if __name__ = = "__main__" :
Str = "11101110"
print (longest_substring_length( Str ))
|
C#
using System;
class Program
{
static int LongestSubstringLength( string str)
{
int count = 0;
int maxCount = 0;
foreach ( char c in str)
{
if (c == '1' )
{
count++;
}
else
{
maxCount = Math.Max(count, maxCount);
count = 0;
}
}
maxCount = Math.Max(count, maxCount);
return maxCount;
}
static void Main()
{
string str = "11101110" ;
Console.WriteLine(LongestSubstringLength(str));
}
}
|
Javascript
function longestSubstringLength(Str) {
let count = 0;
let maxCount = 0;
for (let c of Str) {
if (c == '1' ) {
count++;
} else {
maxCount = Math.max(count, maxCount);
count = 0;
}
}
maxCount = Math.max(count, maxCount);
return maxCount;
}
let Str = "11101110" ;
console.log(longestSubstringLength(Str));
|
Time Complexity: O(n), where n is the length of the binary string.
Space Complexity: O(1).
Last Updated :
11 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...