Find the last remaining Character in the Binary String according to the given conditions


A binary string is a string that only has two characters, usually the numbers 0 and 1, and it represents a series of binary digits.

Problem Statement

Now, in this problem, we are given a binary string comprising zeros and ones. We have two conditions to keep in mind while solving the problem. First, one digit can delete another digit that is a ‘1’ can delete a ‘0’ and vice versa. Secondly, if at any moment the entire string consists of only 0’s and 1’s then the corresponding digit is printed. Here, a binary string will be the input given to us by the user. Let us see how we should proceed to solve this problem.

For Examples

Let’s try to understand this problem with the help of some examples.

Input: s = "100100"
Output: 0

Explanation

Here, 1st digit is 1 and it will delete 2nd digit 0 // 10100

Now, 3rd digit is 0 and it will delete 1st digit 1 // 0100

Now, 4th digit is 1 and it will delete 5th digit 0 // 010

Now, 6th digit is 0 and it will delete 4th digit that is 1 // 00

Now, as both are zeros and no 1 is left so it will return us the final output as 0

Input: s = "10110"
Output: 1

Explanation

Here, 1st digit is 1 and it will delete 2nd digit 0 // 1110

Now, 5th digit is 0 and it will delete 4th digit 1 // 110

Now, 3rd digit is 1 and it will delete 5th digit 0 // 11

Now, as both are ones and no 0 is left so it will return us the final output as 1

Problem Explanation

Let’s try to understand the problem and find its solution. We know that binary string comprises ones and zeros only.

Solution Approach

Algorithm

  • Define arrays a[2] and count[2] to keep track of deleted characters and remaining characters respectively.

  • Create a queue q to store the characters of the binary string.

  • Iterate through the binary string, converting characters to integers and incrementing the corresponding count in the count.

  • While there are both remaining 0s and 1s −

    • Get the front element t from the queue.

    • If a[t] is greater than 0 −

      • Decrement a[t] to indicate a deleted character.

      • Decrement count[t] to indicate one less remaining character of type t.

    • Else −

      • Increment a[t XOR 1] to indicate that a character of the opposite type can delete the next occurrence of character t.

      • Push t back into the queue.

  • After the loop, if there are more remaining 0s, return "0", otherwise return "1".

Pseudocode

// helper function
function helper(binary_string, N):
Declare array a[2] = {0, 0}
Declare array count[2] = {0, 0}
Declare queue q

for i = 0 to N - 1:
   Convert binary_string[i] to integer and store in x
   Increment count[x]
   Push x into queue q

while count[0] > 0 and count[1] > 0:
   t = q.front() 
   Pop the front element from queue q

   if a[t] > 0:
      Decrement a[t]
      Decrement count[t]
   else:
      Increment a[t XOR 1]
      Push t into queue q

if count[0] > 0:
   Return "0"
else:
   Return "1"

// Main function
function main():
Initialize binary_string = "101101000001"
Set N as the length of binary_string
Print "The last remaining Character in the Binary String is: " + helper(binary_string, N)

Call the main function

Example: C++ Code

#include <bits/stdc++.h>
using namespace std;

// Define a helper function to find the last remaining Character in the Binary String according to the given conditions
string helper(string str, int N){
   // Define an array to delete the counters, this will count the deleted values of each 1's and 0's 
   int a[] = { 0, 0 };
   // Define another array that would keep count of the characters left from each 1's and 0's
   int count[] = { 0, 0 };
   // Define a queue to store the given string
   queue<int> q;
   // Run a for loop to initialize the queue
   for (int i = 0; i < N; i++){
      // change character to integer type
      int x = str[i]-'0';
      // Add in counter array followed by pushing the element in the queue
      count[x]++;
      q.push(x);
   }
   // Run another loop till at least 1 digit is left from both 0's and 1's
   while (count[0] > 0 && count[1] > 0){
      // Store the front element in a variable
      int t = q.front();
      q.pop();
      // We will increment the deleted counter for the opposite digit without a floating removal for the current character else delete the current character and move forward
      if (a[t] > 0){
         a[t]--;
         count[t]--;
      }
      else{
         a[t ^ 1]++;
         q.push(t);
      }
   }
   // If at the end 0 are left then the answer is 0, otherwise the answer is 1
   if (count[0] > 0){
      return "0";
   }
   return "1";
}
// Main function
int main(){
   // Input string given for an example
   string s = "101101000001";
   // Store size of String in a variable
   int size = s.length();
   // Call the helper function that returns the last remaining Character in the Binary String
   cout << "The last remaining Character in the Binary String is: " << helper(s, size);
   return 0;
}

Output

The last remaining Character in the Binary String is: 1

Complexities for the above code

Time complexity − O(n); where n denotes the number of digits in the given string.

Space complexity − O(n); We have stored the string in a queue to proceed with the problem in the above code.

Conclusion

In this article, find the last remaining character in the Binary String according to the given conditions. The above solution uses a queue and arrays to keep track of counts and deletion states. It iterates through the binary string, applying the rules of character deletion until there are no adjacent duplicates. The algorithm processes the string efficiently, removing characters based on the given conditions. The final result is the last remaining character in the string after these operations. This approach ensures that the solution runs in a time complexity of O(N), where N is the length of the binary string.

Updated on: 23-Oct-2023

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements