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 “00”.
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:
- Check if str[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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the last removed // character from the string char lastRemovedCharacter(string str) { // Stores length of the string int n = str.length(); // Base Case: if (n == 1) return str[0]; // If the second last // character is '0' if (str[n - 2] == '0' ) { return ( '1' - str[n - 1] + '0' ); } // If the second last // character is '1' else return str[n - 1]; } // Driver Code int main() { string str = "10010" ; cout << lastRemovedCharacter(str); } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to find the last removed // character from the String static char lastRemovedCharacter( char []str) { // Stores length of the String int n = str.length; // Base Case: if (n == 1 ) return str[ 0 ]; // If the second last // character is '0' if (str[n - 2 ] == '0' ) { return ( char )( '1' - str[n - 1 ] + '0' ); } // If the second last // character is '1' else return str[n - 1 ]; } // Driver Code public static void main(String[] args) { String str = "10010" ; System.out.print(lastRemovedCharacter(str.toCharArray())); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to implement # the above approach # Function to find the last removed # character from the string def lastRemovedCharacter( str ): # Stores length of the string n = len ( str ) # Base Case: if (n = = 1 ): return ord ( str [ 0 ]) # If the second last # character is '0' if ( str [n - 2 ] = = '0' ): return ( ord ( '1' ) - ord ( str [n - 1 ]) + ord ( '0' )) # If the second last # character is '1' else : return ord ( str [n - 1 ]) # Driver Code if __name__ = = '__main__' : str = "10010" print ( chr (lastRemovedCharacter( str ))) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to find the last removed // character from the String static char lastRemovedCharacter( char []str) { // Stores length of the String int n = str.Length; // Base Case: if (n == 1) return str[0]; // If the second last // character is '0' if (str[n - 2] == '0' ) { return ( char )( '1' - str[n - 1] + '0' ); } // If the second last // character is '1' else return str[n - 1]; } // Driver Code public static void Main() { string str = "10010" ; Console.Write(lastRemovedCharacter( str.ToCharArray())); } } // This code is contributed by code_hunt |
0
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.