Removing elements between the two zeros
Last Updated :
07 Oct, 2022
Given an integer N which shows the size of the string and in the next line given a string which contains a string of character with only zero and one. The task is to remove a single character each time that comes in between the two zero characters.
During each turn, only one character from the string will be removed that satisfies the following condition :
- It must be surrounded by zeroes on both sides.
Examples:
Input : str = "1001
Output : str = "1001"
Input : str = "10101
Output : str = "1001"
Use a loop from 1 to N - 1 and check if any element lies between two zeros such that s[i - 1] = '0' and s[i + 1] = '0'. If the condition is satisfied then, delete the character at that position, ad start searching for patterns again.
C++
// C++ program to delete elements between zeros
#include <bits/stdc++.h>
using namespace std;
// Function to find the string
// after operation
string findstring(string s)
{
int n = s.length();
// Traversing through string
for (int i = 1; i < n - 1; i++)
{
// Checking for character
// Between two zeros
if ((s.at(i - 1) == '0' &&
s.at(i + 1) == '0'))
{
// deleting the character
// At specific position
s.erase(i, 1);
i--;
if (i > 0 && s.at(i - 1) == '0')
i--;
// updating the length
// of the string
n = s.length();
}
}
return s;
}
// Drivers code
int main() {
cout << findstring("100100");
return 0;
}
Java
// Java program to delete elements between zeros
import java.util.*;
public class GFG
{
// Function to find the string
// after operation
static String findstring(String s)
{
int n = s.length();
// use for loop to remove the
// character between two zeros
for (int i = 1; i < n - 1; i++)
{
// Checking for character
// Between two zeros
if ((s.charAt(i - 1) == '0' &&
s.charAt(i + 1) == '0'))
{
// deleting the character
// At specific position
s = s.substring(0, i) + s.substring(i + 1);
i--;
if (i > 0 && s.charAt(i - 1) == '0')
i--;
// updating the length
// of the string
n = s.length();
}
}
return s;
}
// Driver code
public static void main(String[] args)
{
String s="100100";
System.out.println(findstring(s));
}
}
Python3
# Python3 program to delete elements
# between zeros
# Function to find the string
# after operation
def findstring(s):
n = len(s)
s = list(s)
i = 1
# Traversing through string
while i < n - 1:
# Checking for character
# Between two zeros
if (s[i - 1] == '0' and
s[i + 1] == '0'):
# Deleting the character
# At specific position
s.pop(i)
i -= 1
if i > 0 and s[i - 1] == '0':
i -= 1
# Updating the length
# of the string
n = len(s)
i += 1
return ''.join(s)
# Driver code
if __name__ == '__main__':
print (findstring('100100'))
# This code is contributed by rutvik_56
C#
// C# program to delete
// elements between zeros
using System;
class GFG
{
// Function to find the
// string after operation
static string findstring(string s)
{
int n = s.Length;
string st = "";
// Traversing through string
for (int i = 1; i < n - 1; i++)
{
// Checking for character
// Between two zeros
if ((s[i - 1] == '0' &&
s[i + 1] == '0'))
{
// deleting the character
// At specific position
st = s.Remove(i, 1);
s = st;
i--;
if (i > 0 &&
s[i - 1] == '0')
i--;
// updating the length
// of the string
n = s.Length;
}
}
return s;
}
// Driver code
static void Main()
{
Console.Write(findstring("100100"));
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
JavaScript
<script>
// JavaScript program to delete elements
// between zeros
// Function to find the string
// after operation
function findstring(s){
let n = s.length
s = s.split('')
let i = 1
// Traversing through string
while(i < n - 1){
// Checking for character
// Between two zeros
if (s[i - 1] == '0' && s[i + 1] == '0'){
// Deleting the character
// At specific position
s.splice(i,1);
i -= 1
if(i > 0 && s[i - 1] == '0')
i -= 1
// Updating the length
// of the string
n = s.length
}
i += 1
}
return s.join('')
}
// Driver code
document.write(findstring('100100'),"</br>")
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(N2), where N is the size of the input string and using erase function to delete the value in the string.
Auxiliary Space: O(1)
Similar Reads
Double the first element and move zero to end For a given array of n integers and assume that '0' is an invalid number and all others a valid number. Convert the array in such a way that if both current and next element is valid and both have same value then double current value and replace the next number with 0. After the modification, rearra
14 min read
Remove Leading Zeros From String in C++ Given a string of digits, remove leading zeros from it. Examples: Input : 00000123569 Output : 123569 Input : 000012356090 Output : 12356090 In this article, we will use two string functions i.e, string erase and stoi() to remove leading zeros from the string. 1. Using String Erase FunctionCount tra
2 min read
Remove Leading Zeros From String in Java Given a string of digits, remove leading zeros from it.Illustrations: Input : 00000123569Output: 123569Input: 000012356090Output: 12356090Approach: We use the StringBuffer class as Strings are immutable.Count leading zeros by iterating string using charAt(i) and checking for 0 at the "i" th indices.
3 min read
Move all zeroes to end of number Given a number N, the task is to move all zeroes in the number to the end. Examples: Input: N = 230404Output: 234400Explanation: After moving all the zeros to the end, 230404 becomes 234400 Input: N = 1004Output: 1400Explanation: After moving all the zeros to the end, 1004 becomes 1400. Approach: To
3 min read
Check whether a + b = c or not after removing all zeroes from a,b and c Given two integers a and b, the task is to add them to get c. After that remove zeroes from a, b and c and check for modified values if a + b = c then return "YES" else return "NO".Examples: Input: a = 101, b = 102 Output: YES 101 + 102 = 203. After removing all zeroes from a, b and c, a = 11, b = 1
8 min read