📌  相关文章
📜  生成由给定字符串的字符确定的序列

📅  最后修改于: 2021-10-26 05:52:00             🧑  作者: Mango

给定长度为N ( 1 ≤ N ≤ 10 5 ) 的二进制字符串S ,任务是打印由满足以下条件的整数0N组成的排列A

  • 如果S[i] = 1 ,则A[i] < A[i + 1]
  • 如果S[i] = 0 ,则A[i] > A[i + 1]

例子:

处理方法:按照以下步骤解决问题:

  • 分配的最低数量的可能,如果在字符串中遇到的字符是“1”。
  • 分配次数最多的可能的,如果在字符串中遇到的字符是“0”。
  • 设置两个指针lo (= 0) ,存储当前可能的最低值,和hi (= N) ,存储当前可能的最高值。
  • 遍历字符串并将值附加到结果数组,比如re s,从相应的范围使用两个指针。
  • 遍历完字符串,只剩下一个值需要追加。将lo附加到最后一个索引。
  • 打印数组res作为所需的结果。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print the
// required sequence
void StringPattern(string S)
{
    // Length of the string
    int n = (int)S.size();
 
    // Pointers to store lowest and
    // highest possible values
    int l = 0, r = n;
 
    // Stores the required answer
    vector res(n + 1, 0);
 
    for (int i = 0; i < n; i++) {
 
        switch (S[i]) {
 
        // If current character is '1'
        case '1':
 
            // Assign smallest
            // possible value
            res[i] = l++;
            break;
 
        // If current character is '0'
        case '0':
 
            // Assign largest
            // possible value
            res[i] = r--;
            break;
        }
    }
 
    // At this point, l == r ,
    // only one value is not assigned yet.
    res[n] = l;
    for (int el : res) {
        cout << el << " ";
    }
}
 
// Driver Code
int main()
{
    string s = "001";
    StringPattern(s);
 
    return 0;
}


Java
// Java Implementation of above approach
import java.util.*;
class GFG
{
 
// function to find minimum required permutation
static void StringPattern(String s)
{
    int n = s.length();
    int l = 0, r = n; 
    int [] res = new int[n + 1];
    for (int x = 0; x < n; x++)
    {
        if (s.charAt(x) == '1')
        {
            res[x] = l++;
        }
        else if(s.charAt(x) == '0')
        {
            res[x] = r--;
        }
    }    
    res[n] = l;
    for(int i = 0; i < n+1; i++)
    {
        System.out.print(res[i]+" ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    String s = "001";
    StringPattern(s);
}
}
 
// This code is contributed by mohit kumar 29.


Python3
# Python program to implement
# the above approach
 
# Function to print the
# required sequence
def StringPattern(S):
 
    # Stores the lowest, highest
    # possible values that can be
    # appended, size of the string
    lo, hi = 0, len(S)
 
    # Stores the required result
    ans = []
 
    # Traverse the string
    for x in S:
 
        # If current character is '1'
        if x == '1':
 
            # Append lowest
            # possible value
            ans.append(lo)
 
            lo += 1
 
        # If current character is '0'
        else:
 
            # Append highest
            # possible value
            ans.append(hi)
 
            hi -= 1
 
    return ans + [lo]
 
 
# Driver Code
if __name__ == "__main__":
        # Input
    s = '001'
    print(StringPattern(s))


C#
// C# Implementation of above approach
using System;
class GFG
{
 
// function to find minimum required permutation
static void StringPattern(String s)
{
    int n = s.Length;
    int l = 0, r = n; 
    int [] res = new int[n + 1];
    for (int x = 0; x < n; x++)
    {
        if (s[x] == '1')
        {
            res[x] = l++;
        }
        else if(s[x] == '0')
        {
            res[x] = r--;
        }
    }    
    res[n] = l;
    for(int i = 0; i < n + 1; i++)
    {
        Console.Write(res[i] + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "001";
    StringPattern(s);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3 2 0 1

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程