Check if a Binary String can be sorted in decreasing order by removing non-adjacent characters
Given a binary string S of size N, the task is to check if the binary string S can be sorted in decreasing order by removing any number of the non-adjacent characters. If it is possible to sort the string in decreasing order, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “10101011011”
Output: Yes
Explanation:
After removing the non-adjacent characters at indices 1, 3, 5, and 8 modifies the string to “1111111”, which is sorted in decreasing order. Therefore, print “Yes”.
Input: S = “0011”
Output: No
Approach: The given problem can be solved based on the observations that if there exist two adjacent characters having 1s and then there exists adjacent characters having 0s then it is impossible to sort the string by removing the non-adjacent characters. Follow the steps below to solve the problem:
- Initialize a boolean variable, say flag as true that stores the status whether the given string can be sorted or not.
- Traverse the given string S from the end and if there exists any pairs of adjacent characters having values 1s then stored the second index of 1 in a variable say idx and break out of the loop.
- Traverse the given string S again from the back over the range [idx, 0] and if there exists any pairs of adjacent characters having values 0s then update the value of flag as false and break out of the loop.
- After completing the above steps, if the value of flag is true, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string canSortString(string S, int N)
{
int flag = 1;
int i, j;
for (i = N - 2; i >= 0; i--) {
if (S[i] == '1'
&& S[i + 1] == '1' ) {
break ;
}
}
for ( int j = i; j >= 0; j--) {
if (S[j] == '0'
&& S[j + 1] == '0' ) {
flag = 0;
break ;
}
}
if (flag == 0) {
return "No" ;
}
else {
return "Yes" ;
}
}
int main()
{
string S = "10101011011" ;
int N = S.length();
cout << canSortString(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String canSortString(String S, int N)
{
int flag = 1 ;
int i, j;
for (i = N - 2 ; i >= 0 ; i--) {
if (S.charAt(i) == '1'
&& S.charAt(i + 1 ) == '1' ) {
break ;
}
}
for ( j = i; j >= 0 ; j--) {
if (S.charAt(j) == '0'
&& S.charAt(j + 1 ) == '0' ) {
flag = 0 ;
break ;
}
}
if (flag == 0 ) {
return "No" ;
}
else {
return "Yes" ;
}
}
public static void main (String[] args) {
String S = "10101011011" ;
int N = S.length();
System.out.println(canSortString(S, N));
}
}
|
Python3
def canSortString(S, N):
flag = 1
i = N - 2
while (i > = 0 ):
if (S[i] = = '1' and S[i + 1 ] = = '1' ):
break
i - = 1
j = i
while (j > = 0 ):
if (S[j] = = '0' and S[j + 1 ] = = '0' ):
flag = 0
break
j - = 1
if (flag = = 0 ):
return "No"
else :
return "Yes"
if __name__ = = '__main__' :
S = "10101011011"
N = len (S)
print (canSortString(S, N))
|
C#
using System;
class GFG{
static string canSortString( string S, int N)
{
int flag = 1;
int i, j;
for (i = N - 2; i >= 0; i--)
{
if (S[i] == '1' && S[i + 1] == '1' )
{
break ;
}
}
for (j = i; j >= 0; j--)
{
if (S[j] == '0' && S[j + 1] == '0' )
{
flag = 0;
break ;
}
}
if (flag == 0)
{
return "No" ;
}
else
{
return "Yes" ;
}
}
public static void Main( string [] args)
{
string S = "10101011011" ;
int N = S.Length;
Console.WriteLine(canSortString(S, N));
}
}
|
Javascript
<script>
function canSortString(S, N)
{
let flag = 1;
let i, j;
for (let i = N - 2; i >= 0; i--) {
if (S[i] == '1'
&& S[i + 1] == '1' ) {
break ;
}
}
for (let j = i; j >= 0; j--) {
if (S[j] == '0'
&& S[j + 1] == '0' ) {
flag = 0;
break ;
}
}
if (flag == 0) {
return "No" ;
}
else {
return "Yes" ;
}
}
let S = "10101011011" ;
let N = S.length;
document.write(canSortString(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
25 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...