📌  相关文章
📜  检查二进制字符串是否可以通过删除不相邻的字符按降序排序

📅  最后修改于: 2021-09-04 08:14:19             🧑  作者: Mango

给定一个大小为N的二进制字符串S ,任务是检查二进制字符串S 是否可以通过删除任意数量的非相邻字符来按降序排序。如果可以按降序对字符串进行排序,则打印“Yes” 。否则,打印“否”

例子:

方法:根据观察结果可以解决给定的问题,如果存在两个相邻的字符为1 ,然后相邻的字符为0 ,则无法通过删除非相邻字符来对字符串进行排序。请按照以下步骤解决问题:

  • 初始化一个布尔变量,比如flagtrue ,它存储给定字符串是否可以排序的状态。
  • 从末尾遍历给定的字符串S ,如果存在任何值为1s的相邻字符对,则将第二个索引1存储在变量中,例如idx并跳出循环。
  • 在范围[idx, 0] 上再次从后面遍历给定的字符串S ,如果存在任何值为0的相邻字符对,则将flag的值更新为false并退出循环。
  • 完成上述步骤后,如果flag 的值为true ,则打印“Yes” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ programm for the above appraoch
 
#include 
using namespace std;
 
// Function to sort the given string in
// decreasing order by removing the non
// adajcent characters
string canSortString(string S, int N)
{
    // Keeps the track whether the
    // string can be sorted or not
    int flag = 1;
 
    int i, j;
 
    // Traverse the given string S
    for (i = N - 2; i >= 0; i--) {
 
        // Check if S[i] and
        // S[i + 1] are both '1'
        if (S[i] == '1'
            && S[i + 1] == '1') {
            break;
        }
    }
 
    // Traverse the string S from
    // the indices i to 0
    for (int j = i; j >= 0; j--) {
 
        // If S[j] and S[j + 1] is
        // equal to 0
        if (S[j] == '0'
            && S[j + 1] == '0') {
 
            // Mark flag false
            flag = 0;
            break;
        }
    }
 
    // If flag is 0, then it is not
    // possible to sort the string
    if (flag == 0) {
        return "No";
    }
 
    // Otherwise
    else {
        return "Yes";
    }
}
 
// Driver Code
int main()
{
    string S = "10101011011";
    int N = S.length();
    cout << canSortString(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to sort the given string in
// decreasing order by removing the non
// adajcent characters
static String canSortString(String S, int N)
{
   
    // Keeps the track whether the
    // string can be sorted or not
    int flag = 1;
 
    int i, j;
 
    // Traverse the given string S
    for (i = N - 2; i >= 0; i--) {
 
        // Check if S[i] and
        // S[i + 1] are both '1'
        if (S.charAt(i) == '1'
            && S.charAt(i + 1) == '1') {
            break;
        }
    }
 
    // Traverse the string S from
    // the indices i to 0
    for ( j = i; j >= 0; j--) {
 
        // If S[j] and S[j + 1] is
        // equal to 0
        if (S.charAt(j) == '0'
            && S.charAt(j + 1) == '0') {
 
            // Mark flag false
            flag = 0;
            break;
        }
    }
 
    // If flag is 0, then it is not
    // possible to sort the string
    if (flag == 0) {
        return "No";
    }
 
    // Otherwise
    else {
        return "Yes";
    }
}
 
    public static void main (String[] args) {
      String S = "10101011011";
    int N = S.length();
    System.out.println(canSortString(S, N));
   
    }
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above appraoch
 
# Function to sort the given string in
# decreasing order by removing the non
# adajcent characters
def canSortString(S, N):
     
    # Keeps the track whether the
    # string can be sorted or not
    flag = 1
 
    # Traverse the given string S
    i = N - 2
     
    while(i >= 0):
         
        # Check if S[i] and
        # S[i + 1] are both '1'
        if (S[i] == '1' and S[i + 1] == '1'):
            break
         
        i -= 1
 
    # Traverse the string S from
    # the indices i to 0
    j = i
     
    while(j >= 0):
         
        # If S[j] and S[j + 1] is
        # equal to 0
        if (S[j] == '0' and S[j + 1] == '0'):
             
            # Mark flag false
            flag = 0
            break
         
        j -= 1
 
    # If flag is 0, then it is not
    # possible to sort the string
    if (flag == 0):
        return "No"
 
    # Otherwise
    else:
        return "Yes"
 
# Driver Code
if __name__ == '__main__':
     
    S = "10101011011"
    N = len(S)
     
    print(canSortString(S, N))
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to sort the given string in
// decreasing order by removing the non
// adajcent characters
static string canSortString(string S, int N)
{
     
    // Keeps the track whether the
    // string can be sorted or not
    int flag = 1;
 
    int i, j;
 
    // Traverse the given string S
    for(i = N - 2; i >= 0; i--)
    {
         
        // Check if S[i] and
        // S[i + 1] are both '1'
        if (S[i] == '1' && S[i + 1] == '1')
        {
            break;
        }
    }
 
    // Traverse the string S from
    // the indices i to 0
    for(j = i; j >= 0; j--)
    {
         
        // If S[j] and S[j + 1] is
        // equal to 0
        if (S[j] == '0' && S[j + 1] == '0')
        {
             
            // Mark flag false
            flag = 0;
            break;
        }
    }
 
    // If flag is 0, then it is not
    // possible to sort the string
    if (flag == 0)
    {
        return "No";
    }
 
    // Otherwise
    else
    {
        return "Yes";
    }
}
 
// Driver code
public static void Main(string[] args)
{
    string S = "10101011011";
    int N = S.Length;
     
    Console.WriteLine(canSortString(S, N));
}
}
 
// This code is contributed by ukasp


Javascript


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live