Make Binary Strings equal by replacing 2nd bit repeatedly
Given two binary strings S and T of size N and M respectively, the task is to check if S can be converted into T by doing the following operations, any number of times:
- Suppose a is the first character of S and b is the second character of S.
- Change the second character of S to either min(a, b) or max(a, b),
- and remove the first character.
- Note that the length of the string reduces by 1 after each operation.
Examples:
Input: S = “001001” , T = “11”
Output: YES
Explanation: Just do the max operation (change second character of S to max(first character, second character) and remove the first character 4 times on the string S.
- “001001″ -> “01001″
- “01001″ -> “1001″
- “1001″ -> “101″
- “101″ -> “11″ , which is equal to the string T
Input: S = “000001” , T = “11”
Output: NO
Approach:
- It can be observed that only initial N – M + 1 elements of S can be modified (else length of S would not be equal to T).
- Therefore, the substring S[N – M + 2]…S[N] must be equal to T[2]…T[M]
- And for the first element of T, if it is 0, min operation can be used on the elements of S.
- Else, the max operation can be used.
So it would be sufficient to check whether any of the initial N – M + 1 elements of S equals first element of T.
Based on the above observation, the following approach can be used to solve the problem :
- If the length of S is less than the length of T, return false.
- Iterate from i = 1 to M – 1 and check if S[i + N – M] is equal to T[i] at each iteration. If not so, return false.
- Iterate from 0 to N – M and check if S[i] is equal to T[0] at any iteration and above conditions are satisfied, return true.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible(string S, string T)
{
int N = S.size(), M = T.size();
if (N < M) {
return false ;
}
for ( int i = 1; i < M; i++) {
if (S[i + N - M] != T[i]) {
return false ;
}
}
for ( int i = 0; i < N - M + 1; i++) {
if (S[i] == T[0]) {
return true ;
}
}
return false ;
}
int main()
{
string S = "001001" ;
string T = "11" ;
bool answer = isPossible(S, T);
if (answer == true ) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
}
|
Java
import java.io.*;
class GFG {
public static boolean isPossible(String S, String T)
{
int N = S.length(), M = T.length();
if (N < M) {
return false ;
}
for ( int i = 1 ; i < M; i++) {
if (S.charAt(i + N - M) != T.charAt(i)) {
return false ;
}
}
for ( int i = 0 ; i < N - M + 1 ; i++) {
if (S.charAt(i) == T.charAt( 0 )) {
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
String S = "001001" ;
String T = "11" ;
boolean answer = isPossible(S, T);
if (answer == true ) {
System.out.print( "YES" );
}
else {
System.out.print( "NO" );
}
}
}
|
Python3
def isPossible(S, T) :
N = len (S);
M = len (T);
if (N < M) :
return False ;
for i in range ( 1 , M) :
if (S[i + N - M] ! = T[i]) :
return False ;
for i in range (N - M + 1 ) :
if (S[i] = = T[ 0 ]) :
return True ;
return False ;
if __name__ = = "__main__" :
S = "001001" ;
T = "11" ;
answer = isPossible(S, T);
if (answer = = True ) :
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
public static bool isPossible( string S, string T)
{
int N = S.Length, M = T.Length;
if (N < M) {
return false ;
}
for ( int i = 1; i < M; i++) {
if (S[i + N - M] != T[i]) {
return false ;
}
}
for ( int i = 0; i < N - M + 1; i++) {
if (S[i] == T[0]) {
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
String S = "001001" ;
String T = "11" ;
bool answer = isPossible(S, T);
if (answer == true ) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function isPossible(S, T) {
let N = S.length, M = T.length;
if (N < M) {
return false ;
}
for (let i = 1; i < M; i++) {
if (S[i + N - M] != T[i]) {
return false ;
}
}
for (let i = 0; i < N - M + 1; i++) {
if (S[i] == T[0]) {
return true ;
}
}
return false ;
}
let S = "001001" ;
let T = "11" ;
let answer = isPossible(S, T);
if (answer == true ) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
31 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...