


In this problem, we need to sort the given binary string in descending order by removing only non-adjacent elements.
To solve this problem, we need to remove all 0s that precede 1 in the binary string. If we find two consecutive zeros followed by two consecutive 1s anywhere in the string, it means we cannot sort the string in descending order. Otherwise, we can classify each situation.
Problem Statement - We are given a binary string str of length equal to N. We need to check if a given string can be sorted in descending order by removing multiple non-adjacent characters from the string. the given string. If the string can be sorted in descending order, print "yes"; otherwise, print "no".
Example
Input – str = "10101100"
Output – “YES”
illustrate
We can sort the string in descending order by removing "0" from the second and fourth position.
Input – str = "11000"
Output – “YES”
illustrate
Strings have been sorted.
Input – str = “010001100”
Output – “NO”
illustrate
Here, we need to remove the 0s at the 1st, 3rd, 4th, and 5th positions to sort the string, but we cannot remove adjacent 0s. Alternatively, we could sort the string by removing all "1"s, but this is also impossible because two "1"s are adjacent.
method 1
In this method we will iterate through the string starting from the end. If we find two consecutive "1"s, we break the loop. After that, we check if the string contains two consecutive "0"s. If so, we return false. Otherwise, we return true.
algorithm
Step 1 - Start using a for loop to iterate through the string from 'len – 2' to 0. Here, 'len' is the length of the given binary string.
-
Step 2 - If both str[i] and str[i 1] are equal to "1", terminate the loop using the "break" keyword.
Step 3 - Now, start traversing the string starting from the i-th index.
Step 4 - If str[j] and str[j 1] are both equal to '0', return 0. Returns 1 if the loop terminates successfully. p>
Step 5 - Print "YES" or "NO" in the driver code based on the return value of the isSortPossible() function.
Example
#include <bits/stdc++.h> using namespace std; // removing the non-adjacent characters from the given string to make it sorted in descending order bool isSortPossible(string str, int len){ int i, j; // Traverse the string str from the end for (i = len - 2; i >= 0; i--){ // if str[i] and str[i + 1] is equal to 1 then break the loop. if (str[i] == '1' && str[i + 1] == '1'){ break; } } // start traversing the string from i for (int j = i; j >= 0; j--){ // If str[j] and str[j + 1] is equal to 0 then return false if (str[j] == '0' && str[j + 1] == '0'){ return 0; } } return 1; } int main(){ string str = "10101100"; int len = str.length(); cout << "The sorting of the given binary string is possible by removing non-adjacent characters - " << endl; if (isSortPossible(str, len)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
Output
The sorting of the given binary string is possible by removing non-adjacent characters − YES
Time complexity - O(N), when we iterate over the string.
Space complexity - O(1)
Method 2
In this method we will use the same logic as the first method, but we have optimized the code to make it more readable. Here, instead of using two separate loops, we will use only a single loop to detect two consecutive "1"s after two consecutive "0"s.
algorithm
Step 1 - Define the "isTwoZeros" variable and initialize it with the "false" value.
Step 2 - Start iterating the string from index 0 to "len – 1".
Step 3 - If str[i] and str[I 1] are "0" and "isTwoZeros" equals false, then change the value of "isTwoZeros" to true . This means we got two consecutive zeros in the given string.
Step 4 - In the else part, if str[i] and str[I 1] are '1' and 'isTwoZeros' equals true, then from the function. This means we get two consecutive "1"s after two consecutive zeros.
Step 5 - Return true when all iterations of the for loop terminate.
Example
#include <bits/stdc++.h> using namespace std; // removing the non-adjacent characters from the given string to make it sorted in descending order bool isSortPossible(string str, int len){ // to track if there are two adjacent zeros in the string bool isTwoZeros = false; // traverse the string for (int i = 0; i < len - 1; i++){ // if two zeros are adjacent, then change the value of isTwoZeros to true if (str[i] == '0' && str[i + 1] == '0' && !isTwoZeros){ isTwoZeros = true; } else{ // if we find two adjacent ones after finding two adjacent zeros, then return false if (str[i] == '1' && str[i + 1] == '1' && isTwoZeros){ return false; } } } // return true if we don't find two adjacent ones after finding two adjacent zeros return true; } int main(){ string str = "101001100"; int len = str.length(); cout << "The sorting of the given binary string is possible by removing non-adjacent characters - " << endl; if (isSortPossible(str, len)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
Output
The sorting of the given binary string is possible by removing non-adjacent characters - NO
Time complexity - O(N)
Space complexity - O(1)
in conclusion
We learned two ways to sort a binary string in descending order by removing only non-adjacent characters. Both methods use the same logic with minimal changes to the code. The code for the second approach is more readable than the code for the first approach.
The above is the detailed content of Check if a binary string can be sorted in descending order by removing non-adjacent characters. For more information, please follow other related articles on the PHP Chinese website!
![从 Windows 10/11 中删除用户帐户的 5大方法 [2023]](https://img.php.cn/upload/article/000/465/014/168782606547724.png)
您的WindowsPC上有多个过时的帐户?或者,由于某些错误,您是否在从系统中删除这些帐户时陷入困境?无论出于何种原因,您都应该尽快从计算机中删除那些未使用的用户帐户。这样,您将节省大量空间并修复系统中可能的漏洞点。在本文中,我们通过详细步骤详细阐述了多种用户帐户删除方法。方法1–使用设置这是从系统中删除任何帐户的标准方法。步骤1–按Win+I键应打开“设置”窗口。步骤2–转到“帐户”。第3步–找到“其他用户”将其打开。第4步–您将在屏幕右侧找到所有帐户。步骤5–只需在那里扩展帐户即可。在帐户和

windows7系统如何删除administrator账户呢?很多用户的电脑当中都有多个administrator账户,不过有些账户是使用不到的,所以我们可以删除那些没有必要的管理员账户,那么win7系统如何删除administrator账户呢?今天为大家分享win7系统删除administrator账户的方法。感兴趣的小伙伴们快来看看吧!1、首先,右键点击桌面上的“计算机”图标,菜单栏选择“管理”。2、在计算机管理界面中,依次展开“系统工具——>本地用户——>用户”选项。3、然后在

彻底删除快应用的方法:1、打开手机设置界面,点击打开“应用设置”;2、在应用设置界面,选择“应用管理”点击打开;3、进入应用管理界面,界面选择“快应用服务框架”点击打开;4、进入快应用服务框架界面,选择“卸载更新”选项并打开;5、界面显示窗口点击“确定”即可彻底删除快应用。

使用LinkedList类的removeLast()方法删除链表中的最后一个元素LinkedList是Java集合框架中常见的一种数据结构,它以双向链表的形式存储元素。通过LinkedList类提供的方法,我们可以方便地对链表进行操作,例如添加、删除和修改元素。在某些场景下,我们可能需要删除链表中的最后一个元素。LinkedList类提供了removeLas

PHP函数介绍—unlink():删除文件概述:在PHP中,unlink()是一个删除文件的函数。它可以帮助我们在代码中删除指定路径下的文件。语法:unlink(string$filename[,resource$context]):bool参数:filename:必需,要删除的文件路径,可以是相对路径或绝对路径。context:可

在查看Win7系统的计算机网络连接时,有时会发现除了本地连接外,还有一个本地连接2。了解后,我们知道这是一个多余的网卡本地连接地址。由于本地连接2的存在,有时本地连接无法连接,因此无法上网。我们如何删除多余的本地连接2?Win7系统删除多余的本地连接2。1.点击开始菜单,搜索运行按下回车键或直接按Windows键(窗口)+R键,打开运行对话框;2.在运行对话框中输入regedit,并按确定键;3.在注册表编辑器中,展开到HKEY_LOCAL_MCHINE\SYSTEM\Curentcontrol

许多Windows11用户抱怨由于某种原因无法从他们的PC中删除。这可能很烦人,因为它会阻止用户释放内存或删除不需要的文件。但是,我们将讨论为什么文件不会在Windows11上删除以及如何修复它。另外,您可能对我们的文章感兴趣,如果文件资源管理器删除的文件仍显示在您的计算机上,该怎么办。为什么我的电脑不允许我删除文件?如果您不是文件所有者或您的用户帐户没有适当的访问权限,则可能会发生这种情况。该文件可能正被另一个程序或进程使用,从而阻止其被删除。操作系统或第三方程序可能会锁定文件或文件夹。如果计

如何通过PHPZipArchive实现对压缩包中文件的删除操作?概述:ZipArchive是PHP中用于创建和读取ZIP压缩文件的类。除了创建和读取,ZipArchive还提供了其他一些操作,例如删除、重命名、复制和解压缩等。本文将重点介绍如何使用ZipArchive类来删除压缩包中的文件。步骤:在进行删除操作之前,我们需要先打开压缩包并检查是否存在目标文


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
