Minimize flipping of bits in given Binary string to make count of 10 equal to 01
Given binary string str, the task is to choose any index and change into 0 or 1, and do this in minimum steps such that the count of substring 01 is equal to 10.
Examples:
Input: str = “01101”
Output: 01100
Explanation: 01 as a substring repeat 2 times in a string, 10 as a substring repeat 1 times in a string. So, change last char 1 into 0 then count of 01 and 10 is 1 and equal
Input: str = “01101010”
Output: 01101010
Approach: If we can observe that if the first and last character of the string is the same so the count of “01” is equal to 1 because by induction is always one character is present in the middle of the string so, we can split a string into two parts s[1….i], [i…n] so AB(s) = BA(s).
- If the first and last characters are not the same, then change the first character to the last character.
- After performing the above steps, print the value of str as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string MakeEqual(string str)
{
char FirstChar = str[0];
char LastChar = str[str.size() - 1];
if (FirstChar != LastChar) {
str[0] = LastChar;
}
return str;
}
int main()
{
string str = "0110101" ;
string ans = MakeEqual(str);
cout << ans;
return 0;
}
|
Java
class GFG
{
static String MakeEqual(String str) {
char FirstChar = str.charAt( 0 );
char LastChar = str.charAt(str.length() - 1 );
if (FirstChar != LastChar) {
str = str.substring( 1 , str.length());
str = LastChar + str;
}
return str;
}
public static void main(String args[]) {
String str = "0110101" ;
String ans = MakeEqual(str);
System.out.println(ans);
}
}
|
Python3
def MakeEqual( str ):
FirstChar = str [ 0 ]
LastChar = str [ - 1 ]
if (FirstChar ! = LastChar):
str [ 0 ] = LastChar
return ''.join( str )
if __name__ = = "__main__" :
str = "0110101"
ans = MakeEqual( list ( str ))
print (ans)
|
C#
using System;
public class GFG
{
static String MakeEqual(String str) {
char FirstChar = str[0];
char LastChar = str[str.Length - 1];
if (FirstChar != LastChar) {
str = str.Substring(1, str.Length - 1);
str = LastChar + str;
}
return str;
}
public static void Main(String []args) {
String str = "0110101" ;
String ans = MakeEqual(str);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function MakeEqual(str)
{
str = str.split( '' )
let FirstChar = str[0];
let LastChar = str[str.length - 1];
if (FirstChar != LastChar)
{
str[0] = LastChar;
}
return str.join( '' );
}
let str = "0110101" ;
let ans = MakeEqual(str);
document.write(ans);
</script>
|
Time Complexity: O(1), The time complexity of this program is O(1) because it only performs constant operations like accessing characters in the string, comparing characters, and assigning new values to the string. Therefore, the time taken by the program will be the same for all input sizes.
Auxiliary Space: O(1), The space complexity of this program is O(1) because it only uses a fixed amount of memory to store the input string, two characters for the first and last characters of the string, and a string variable to store the modified string. Therefore, the space used by the program will remain constant for all input sizes.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...