Find the last remaining Character in the Binary String according to the given conditions
Given a binary string str consisting of only 0’s and 1’s. The following two operations can be performed on it:
- One digit can delete another digit i.e. a 0 can delete a 1 and vice versa.
- If at any moment, the entire string consists only 0’s or 1’s, then the respective digit is printed.
The task is to print the remaining digit which will be left at the end.
Examples:
Input: str = “100”
Output: 0
Explanation:
The 1st digit is 1 and it deletes the next digit 0.
The 2nd digit, i.e. 0, is deleted and now does not exists.
Now, the 3rd digit 0 deletes the 1st digit 1.
Since now only 0 is left, the output is 0.
Input: str = “10”
Output: 1
Approach: For this queue data structure is used. The following steps can be followed to compute the answer:
- All the digits are added to the queue.
- Two counters are maintained as an array of size 2 del[2] which will represent the number of floating deletes present for each digit.
- The queue is traversed until there exits at least one digit of both the types.
- Then for each digit in the queue if the delete counter for this digit is not 0, then it is deleted.
- Else, the delete counter for the opposite digit is incremented and placed back into the queue.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string remainingDigit(string S, int N)
{
int del[] = { 0, 0 };
int count[] = { 0, 0 };
queue< int > q;
for ( int i = 0; i < N; i++)
{
int x = S[i] == '1' ? 1 : 0;
count[x]++;
q.push(x);
}
while (count[0] > 0 && count[1] > 0)
{
int t = q.front();
q.pop();
if (del[t] > 0)
{
del[t]--;
count[t]--;
}
else
{
del[t ^ 1]++;
q.push(t);
}
}
if (count[0] > 0)
return "0" ;
return "1" ;
}
int main()
{
string S = "1010100100000" ;
int N = S.length();
cout << remainingDigit(S, N);
}
|
Java
import java.util.*;
public class GfG {
private static String remainingDigit(String S, int N)
{
char c[] = S.toCharArray();
int del[] = { 0 , 0 };
int count[] = { 0 , 0 };
Queue<Integer> q = new LinkedList<>();
for ( int i = 0 ; i < N; i++) {
int x = c[i] == '1' ? 1 : 0 ;
count[x]++;
q.add(x);
}
while (count[ 0 ] > 0 && count[ 1 ] > 0 ) {
int t = q.poll();
if (del[t] > 0 ) {
del[t]--;
count[t]--;
}
else {
del[t ^ 1 ]++;
q.add(t);
}
}
if (count[ 0 ] > 0 )
return "0" ;
return "1" ;
}
public static void main(String args[])
{
String S = "1010100100000" ;
int N = S.length();
System.out.print(remainingDigit(S, N));
}
}
|
Python3
from collections import deque;
def remainingDigit(S, N):
c = [i for i in S]
de = [ 0 , 0 ]
count = [ 0 , 0 ]
q = deque()
for i in c:
x = 0
if i = = '1' :
x = 1
count[x] + = 1
q.append(x)
while (count[ 0 ] > 0 and count[ 1 ] > 0 ):
t = q.popleft()
if (de[t] > 0 ):
de[t] - = 1
count[t] - = 1
else :
de[t ^ 1 ] + = 1
q.append(t)
if (count[ 0 ] > 0 ):
return "0"
return "1"
if __name__ = = '__main__' :
S = "1010100100000"
N = len (S)
print (remainingDigit(S, N))
|
C#
using System;
using System.Collections.Generic;
public class GfG
{
private static String remainingDigit(String S, int N)
{
char []c = S.ToCharArray();
int []del = { 0, 0 };
int []count = { 0, 0 };
List< int > q = new List< int >();
for ( int i = 0; i < N; i++)
{
int x = c[i] == '1' ? 1 : 0;
count[x]++;
q.Add(x);
}
while (count[0] > 0 && count[1] > 0)
{
int t = q[0];
q.RemoveAt(0);
if (del[t] > 0)
{
del[t]--;
count[t]--;
}
else
{
del[t ^ 1]++;
q.Add(t);
}
}
if (count[0] > 0)
return "0" ;
return "1" ;
}
public static void Main(String []args)
{
String S = "1010100100000" ;
int N = S.Length;
Console.Write(remainingDigit(S, N));
}
}
|
Javascript
<script>
function remainingDigit(S,N)
{
let c = S.split( "" );
let del = [ 0, 0 ];
let count = [ 0, 0 ];
let q = [];
for (let i = 0; i < N; i++) {
let x = (c[i] == '1' ? 1 : 0);
count[x]++;
q.push(x);
}
while (count[0] > 0 && count[1] > 0) {
let t = q.shift();
if (del[t] > 0) {
del[t]--;
count[t]--;
}
else {
del[t ^ 1]++;
q.push(t);
}
}
if (count[0] > 0)
return "0" ;
return "1" ;
}
let S = "1010100100000" ;
let N = S.length;
document.write(remainingDigit(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
28 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...