Given a binary string, find if it is possible to make all its digits equal (either all 0’s or all 1’s) by flipping exactly one bit.
Input: 101
Output: Ye
Explanation: In 101, the 0 can be flipped to make it all 1
Input: 11
Output: No
Explanation: No matter whichever digit you flip, you will not get the desired string.
Input: 1
Output: Yes
Explanation: We can flip 1, to make all 0’s
Method 1 (Counting 0’s and 1’s)
If all digits of a string can be made identical by doing exactly one flip, that means the string has all its digits equal to one another except this digit which has to be flipped, and this digit must be different than all other digits of the string. The value of this digit could be either zero or one. Hence, this string will either have exactly one digit equal to zero, and all other digits equal to one, or exactly one digit equal to one, and all other digit equal to zero.
Therefore, we only need to check whether the string has exactly one digit equal to zero/one, and if so, the answer is yes; otherwise the answer is no.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
bool canMakeAllSame(string str)
{
int zeros = 0, ones = 0;
for ( char ch : str)
(ch == '0' ) ? ++zeros : ++ones;
return (zeros == 1 || ones == 1);
}
int main()
{
canMakeAllSame( "101" ) ? printf ( "Yes\n" ) : printf ( "No\n" );
return 0;
}
|
Java
public class GFG {
static boolean canMakeAllSame(String str)
{
int zeros = 0 , ones = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '0' )
++zeros;
else
++ones;
}
return (zeros == 1 || ones == 1 );
}
public static void main(String args[])
{
System.out.println(canMakeAllSame( "101" ) ? "Yes" : "No" );
}
}
|
Python3
def canMakeAllSame( str ):
zeros = 0
ones = 0
for i in range ( 0 , len ( str )):
ch = str [i];
if (ch = = '0' ):
zeros = zeros + 1
else :
ones = ones + 1
return (zeros = = 1 or ones = = 1 );
if (canMakeAllSame( "101" )):
print ( "Yes\n" )
else :
print ( "No\n" )
|
C#
using System;
class GFG {
static bool canMakeAllSame( string str)
{
int zeros = 0, ones = 0;
for ( int i = 0; i < str.Length; i++) {
char ch = str[i];
if (ch == '0' )
++zeros;
else
++ones;
}
return (zeros == 1 || ones == 1);
}
public static void Main()
{
Console.WriteLine(canMakeAllSame( "101" ) ? "Yes" : "No" );
}
}
|
Javascript
<script>
function canMakeAllSame(str)
{
let zeros = 0, ones = 0;
for (let i = 0; i < str.length; i++) {
let ch = str[i];
if (ch == '0' )
++zeros;
else
++ones;
}
return (zeros == 1 || ones == 1);
}
document.write(canMakeAllSame( "101" ) ? "Yes" : "No" );
</script>
|
PHP
<?php
function canMakeAllSame( $str )
{
$zeros = 0;
$ones = 0;
for ( $i =0; $i < strlen ( $str ); $i ++)
{
$ch = $str [ $i ];
if ( $ch == '0' )
++ $zeros ;
else
++ $ones ;
}
return ( $zeros == 1 || $ones == 1);
}
if (canMakeAllSame( "101" ) )
echo "Yes\n" ;
else echo "No\n" ;
return 0;
?>
|
Time complexity : O(n) where n is the length of the string.
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!