Last remaining character after repeated removal of the first character and flipping of characters of a Binary String
Given a Binary string str of length N, the task is to find the last character removed from the string by repeatedly removing the first character of the string and flipping all the characters of the string if the removed character is ‘0’.
Examples:
Input: str = “1001”
Output: ‘0’
Explanation:
Removing str[0] from the string modifies str to “001”.
Removing str[0] from the string modifies str to “10”.
Removing str[0] from the string modifies str to “0”.
Since the last character removed was ‘0’, the required output is ‘0’.
Input: str = “10010”
Output: ‘0’
Naive Approach: The simplest approach to solve this problem is to iterate over the characters of the string. For every character encountered, remove the first character of the string and check if the removed character was ‘0’ or not. If found to be true, then flip all the characters of the string. Finally, print the character which was removed in the last iteration. Follow the steps below to solve the problem:
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observation:
Case 1:
Suppose str = “XXXXXXX0X”, where X is either ‘0’ or ‘1’.
If the character str[N – 2] is flipped, then str[N – 1] must be flipped.
Therefore, if str[N – 2] is ‘0’, then last removed character will be (‘1’ – str[N – 1] + ‘0’).
Case 2:
Suppose str = “XXXXXXX1X”, where X is either ‘0’ or ‘1.
If the character str[N – 2] is flipped, then str[N – 1] must be flipped.
Therefore, if str[N – 2] is ‘1’, then the last removed character will be str[N – 1].
Follow the steps below to solve the problem:
- If N is equal to 1 then answer is str[0] itself, Otherwise.
- Check if str[N – 2] (For N>=2) is ‘1’ or not. If found to be true, then print str[N – 1].
- Otherwise, print (‘1’ – str[N – 1] + ‘0’).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char lastRemovedCharacter(string str)
{
int n = str.length();
if (n == 1)
return str[0];
if (str[n - 2] == '0' ) {
return ( '1' - str[n - 1] + '0' );
}
else
return str[n - 1];
}
int main()
{
string str = "10010" ;
cout << lastRemovedCharacter(str);
}
|
Java
import java.util.*;
class GFG{
static char lastRemovedCharacter( char []str)
{
int n = str.length;
if (n == 1 )
return str[ 0 ];
if (str[n - 2 ] == '0' ) {
return ( char )( '1' - str[n - 1 ] + '0' );
}
else
return str[n - 1 ];
}
public static void main(String[] args)
{
String str = "10010" ;
System.out.print(lastRemovedCharacter(str.toCharArray()));
}
}
|
Python3
def lastRemovedCharacter( str ):
n = len ( str )
if (n = = 1 ):
return ord ( str [ 0 ])
if ( str [n - 2 ] = = '0' ):
return ( ord ( '1' ) -
ord ( str [n - 1 ]) +
ord ( '0' ))
else :
return ord ( str [n - 1 ])
if __name__ = = '__main__' :
str = "10010"
print ( chr (lastRemovedCharacter( str )))
|
C#
using System;
class GFG{
static char lastRemovedCharacter( char []str)
{
int n = str.Length;
if (n == 1)
return str[0];
if (str[n - 2] == '0' )
{
return ( char )( '1' - str[n - 1] + '0' );
}
else
return str[n - 1];
}
public static void Main()
{
string str = "10010" ;
Console.Write(lastRemovedCharacter(
str.ToCharArray()));
}
}
|
Javascript
<script>
function lastRemovedCharacter(str) {
var n = str.length;
if (n == 1) return str[0];
if (str[n - 2] == "0" ) {
return "1" - str[n - 1] + "0" ;
}
else return str[n - 1];
}
var str = "10010" ;
document.write(lastRemovedCharacter(str));
</script>
|
Time Complexity: O(1), The time complexity is O(1), i.e., constant time. This is because the program performs a fixed set of operations on a given input string of a fixed length, regardless of the contents of the string. Therefore, the time complexity is not dependent on the size of the input.
Auxiliary Space: O(1), The space complexity is O(1), i.e., constant space. This is because the program uses a fixed amount of memory to store the input string and some variables for intermediate calculations. The space used by the program does not increase with the size of the input string, and it remains constant for any input string of a fixed length.
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...