给定长度为N ( 1≤N≤10 5 )的二进制字符串S ,任务是打印由整数0到N组成的满足以下条件的排列A :
- 如果S [i] = 1 ,则A [i] 。
- 如果S [i] = 0 ,则A [i]> A [i + 1] 。
例子:
Input: S = “001”
Output: [3, 2, 0, 1]
Explanation:
S[0] = 0 and A[0] (= 3) > A[1] (= 2)
S[1] = 0 and A[1] (= 2) > A[2] (= 0)
S[2] = 1 and A[2] (= 0) > A[3] (= 1)
Input: S = “1010”
Output: [0, 4, 1, 3, 2]
方法:请按照以下步骤解决问题:
- 如果在字符遇到的字符串为‘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
输出:
3 2 0 1
时间复杂度: O(N)
辅助空间: O(N)