# Length of Longest sub-string that can be removed

Given a binary string (consists of only 0 and 1). If there is “100” as a sub-string in the string, then we can delete this sub-string. The task is to find the length of longest sub-string which can be make removed?

Examples:

Input : str = "1011100000100" Output : 6 // Sub-strings present in str that can be make removed // 101{110000}0{100}. First sub-string 110000-->100-->null, // length is = 6. Second sub-string 100-->null, length is = 3 Input : str = "111011" Output : 0 // There is no sub-string which can be make null

We can solve this problem using a container like vector in c++ or ArrayList in Java. Below is the algorithm to solve this problem :

- Take a vector arr of pair type. Each element in arr stores two values character and it’s respective index in string.
- Store pair(‘@’,-1) as a base in arr. Take variable maxlen = 0 which stores the final result.
- Now one by one iterate for all characters in string, make pair of characters and it’s respective index and store it in arr. In parallel also check the condition if after inserting i’th character last three elements of ‘arr’ are making sub-string “100”.
- If sub-string exist then delete it from ‘arr’. Repeat this loop by number of times till you are getting sub-string “100” in arr and make it null by deleting continuously.
- The difference of indexs of i’th character and index of last element currently present in arr after deletion gives the length of sub-string that can be make null by continuous deletion of sub-string “100”, update maxlen.

## C++

`// C++ implementation of program to find the maximum length ` `// that can be removed ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the length of longest sub-string that ` `// can me make removed ` `// arr --> pair type of array whose first field store ` `// character in string and second field stores ` `// corresponding index of that character ` `int` `longestNull(string str) ` `{ ` ` ` `vector<pair<` `char` `,` `int` `> > arr; ` ` ` ` ` `// store {'@',-1} in arr , here this value will ` ` ` `// work as base index ` ` ` `arr.push_back({` `'@'` `, -1}); ` ` ` ` ` `int` `maxlen = 0; ` `// Initialize result ` ` ` ` ` `// one by one iterate characters of string ` ` ` `for` `(` `int` `i = 0; i < str.length(); ++i) ` ` ` `{ ` ` ` `// make pair of char and index , then store ` ` ` `// them into arr ` ` ` `arr.push_back({str[i], i}); ` ` ` ` ` `// now if last three elements of arr[] are making ` ` ` `// sub-string "100" or not ` ` ` `while` `(arr.size()>=3 && ` ` ` `arr[arr.size()-3].first==` `'1'` `&& ` ` ` `arr[arr.size()-2].first==` `'0'` `&& ` ` ` `arr[arr.size()-1].first==` `'0'` `) ` ` ` `{ ` ` ` `// if above condition is true then delete ` ` ` `// sub-string "100" from arr[] ` ` ` `arr.pop_back(); ` ` ` `arr.pop_back(); ` ` ` `arr.pop_back(); ` ` ` `} ` ` ` ` ` `// index of current last element in arr[] ` ` ` `int` `tmp = arr.back().second; ` ` ` ` ` `// This is important, here 'i' is the index of ` ` ` `// current character inserted into arr[] ` ` ` `// and 'tmp' is the index of last element in arr[] ` ` ` `// after continuous deletion of sub-string ` ` ` `// "100" from arr[] till we make it null, difference ` ` ` `// of these to 'i-tmp' gives the length of current ` ` ` `// sub-string that can be make null by continuous ` ` ` `// deletion of sub-string "100" ` ` ` `maxlen = max(maxlen, i - tmp); ` ` ` `} ` ` ` ` ` `return` `maxlen; ` `} ` ` ` `// Driver program to run the case ` `int` `main() ` `{ ` ` ` `cout << longestNull(` `"1011100000100"` `); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of program to find ` `// the maximum length that can be removed ` `import` `java.util.ArrayList; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// User defined class Pair ` ` ` `static` `class` `Pair{ ` ` ` `char` `first; ` ` ` `int` `second; ` ` ` `Pair(` `char` `first, ` `int` `second){ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` ` ` `} ` ` ` ` ` `/* Function to find the length of longest ` ` ` `sub-string that can me make removed ` ` ` `arr --> pair type of array whose first ` ` ` `field store character in string ` ` ` `and second field stores ` ` ` `corresponding index of that character*/` ` ` `static` `int` `longestNull(String str) ` ` ` `{ ` ` ` `ArrayList<Pair> arr = ` `new` `ArrayList<>(); ` ` ` ` ` `// store {'@',-1} in arr , here this value ` ` ` `// will work as base index ` ` ` `arr.add(` `new` `Pair(` `'@'` `, -` `1` `)); ` ` ` ` ` `int` `maxlen = ` `0` `; ` `// Initialize result ` ` ` ` ` `// one by one iterate characters of string ` ` ` `for` `(` `int` `i = ` `0` `; i < str.length(); ++i) ` ` ` `{ ` ` ` `// make pair of char and index , then ` ` ` `// store them into arr ` ` ` `arr.add(` `new` `Pair(str.charAt(i), i)); ` ` ` ` ` `// now if last three elements of arr[] ` ` ` `// are making sub-string "100" or not ` ` ` `while` `(arr.size() >= ` `3` `&& ` ` ` `arr.get(arr.size()-` `3` `).first==` `'1'` `&& ` ` ` `arr.get(arr.size()-` `2` `).first==` `'0'` `&& ` ` ` `arr.get(arr.size()-` `1` `).first==` `'0'` `) ` ` ` `{ ` ` ` `// if above condition is true then ` ` ` `// delete sub-string "100" from arr[] ` ` ` `arr.remove(arr.size() - ` `3` `); ` ` ` `arr.remove(arr.size() - ` `2` `); ` ` ` `arr.remove(arr.size() - ` `1` `); ` ` ` `} ` ` ` ` ` `// index of current last element in arr[] ` ` ` `int` `tmp = arr.get(arr.size() - ` `1` `).second; ` ` ` ` ` `// This is important, here 'i' is the index ` ` ` `// of current character inserted into arr[] ` ` ` `// and 'tmp' is the index of last element ` ` ` `// in arr[] after continuous deletion of ` ` ` `// sub-string "100" from arr[] till we make ` ` ` `// it null, difference of these to 'i-tmp' ` ` ` `// gives the length of current sub-string ` ` ` `// that can be make null by continuous ` ` ` `// deletion of sub-string "100" ` ` ` `maxlen = Math.max(maxlen, i - tmp); ` ` ` `} ` ` ` ` ` `return` `maxlen; ` ` ` `} ` ` ` ` ` `// Driver program to run the case ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `System.out.println(longestNull(` `"1011100000100"` `)); ` ` ` `} ` `} ` `// This code is contributed by Sumit Ghosh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of program to find the maximum length ` `# that can be removed ` ` ` `# Function to find the length of longest sub-that ` `# can me make removed ` `# arr --> pair type of array whose first field store ` `# character in and second field stores ` `# corresponding index of that character ` `def` `longestNull(S): ` ` ` ` ` `arr` `=` `[] ` ` ` ` ` `# store {'@',-1} in arr , here this value will ` ` ` `# work as base index ` ` ` `arr.append([` `'@'` `, ` `-` `1` `]) ` ` ` ` ` `maxlen ` `=` `0` `# Initialize result ` ` ` ` ` `# one by one iterate characters of Sing ` ` ` `for` `i ` `in` `range` `(` `len` `(S)): ` ` ` `# make pair of char and index , then store ` ` ` `# them into arr ` ` ` `arr.append([S[i], i]) ` ` ` ` ` `# now if last three elements of arr[] are making ` ` ` `# sub-"100" or not ` ` ` `while` `(` `len` `(arr)>` `=` `3` `and` ` ` `arr[` `len` `(arr)` `-` `3` `][` `0` `]` `=` `=` `'1'` `and` ` ` `arr[` `len` `(arr)` `-` `2` `][` `0` `]` `=` `=` `'0'` `and` ` ` `arr[` `len` `(arr)` `-` `1` `][` `0` `]` `=` `=` `'0'` `): ` ` ` ` ` `# if above condition is true then delete ` ` ` `# sub-"100" from arr[] ` ` ` `arr.pop() ` ` ` `arr.pop() ` ` ` `arr.pop() ` ` ` ` ` `# index of current last element in arr[] ` ` ` `tmp ` `=` `arr[` `-` `1` `] ` ` ` `# This is important, here 'i' is the index of ` ` ` `# current character inserted into arr[] ` ` ` `# and 'tmp' is the index of last element in arr[] ` ` ` `# after continuous deletion of sub-Sing ` ` ` `# "100" from arr[] till we make it null, difference ` ` ` `# of these to 'i-tmp' gives the length of current ` ` ` `# sub-that can be make null by continuous ` ` ` `# deletion of sub-"100" ` ` ` `maxlen ` `=` `max` `(maxlen, i ` `-` `tmp[` `1` `]) ` ` ` ` ` ` ` `return` `maxlen ` ` ` ` ` `# Driver code ` ` ` `print` `(longestNull(` `"1011100000100"` `)) ` ` ` `# This code is contriuted by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time complexity :** O(n)

**Auxiliary space :** O(n)

This article is contributed by **Shashank Mishra ( Gullu )**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Length of the longest substring that do not contain any palindrome
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest valid substring
- Length of the longest substring with equal 1s and 0s
- Length of the longest substring with no consecutive same letters
- Length of the longest substring without repeating characters
- Find length of longest subsequence of one string which is substring of another string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest Non-palindromic substring
- Longest Palindromic Substring | Set 2
- Longest Common Substring | DP-29
- Longest Palindromic Substring | Set 1
- Longest substring with count of 1s more than 0s
- Longest substring of vowels
- Longest substring having K distinct vowels