📜  二进制字符串的字典排序

📅  最后修改于: 2021-09-04 11:42:34             🧑  作者: Mango

给定一个长度为N的二进制字符串S ,任务是找到给定字符串的字典排序。

例子:

朴素的方法:最简单的方法是生成长度为N 的所有可能的二进制字符串(01组成)并将它们存储在一个数组中。完成后,按字典顺序对字符串数组进行排序,并打印数组中给定字符串的索引。
时间复杂度: O(2 N )
辅助空间: O(1)

有效的方法:这个想法是通过将‘a’ 的每次出现替换为0并将‘b’替换为1来生成由01组成的二进制字符串。因此,列表中的字符串变为:

可以观察到,对于大小为N的字符串,存在(2 N – 2),该给定的字符串之前长度的字符串小于N。让它等于X 。现在,它在长度为N 的字符串的字典位置可以通过将字符串转换为其十进制等效数并加 1 来计算。让它等于Y

下面是上述方法的实现:

C++
//C++ program for the above approach
#include 
using namespace std;
 
// Function to find the rank of a string
void findRank(string s)
{
 
  // Store the length of the string
  int N = s.size();
 
  // Stores its equivalent decimal value
  string bin;
 
  // Traverse the string
  for (int i = 0; i < N; i++)
  {
    if (s[i] == '0')
      bin += "0";
    else
      bin += "1";
  }
   
  // Store the number of strings of
  // length less than N occurring
  // before the given string
  long long X = 1ll<= 0; i--)
  {
    if (bin[i] == '1') 
      res += (val);
 
    val *= 2ll;
  }
 
  long long Y = res;
 
  // Store the rank in answer
  long ans = X + Y - 1;
 
  // Print the answer
  cout << ans;
}
 
// Driver program
int main()
{
  string S = "001";
  findRank(S);
  return 0;
}
 
// This code is contributed by jyoti369.


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
 
    // Function to find the rank of a string
    static void findRank(String s)
    {
        // Store the length of the string
        int N = s.length();
 
        // Stores its equivalent decimal value
        StringBuilder sb = new StringBuilder("");
 
        // Traverse the string
        for (int i = 0; i < N; i++) {
 
            if (s.charAt(i) == '0')
                sb.append('0');
            else
                sb.append('1');
        }
 
        String bin = sb.toString();
 
        // Store the number of strings of
        // length less than N occurring
        // before the given string
        long X = (long)Math.pow(2, N);
 
        // Store the decimal equivalent
        // number of string bin
        long Y = Long.parseLong(bin, 2);
 
        // Store the rank in answer
        long ans = X + Y - 1;
 
        // Print the answer
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "001";
        findRank(S);
    }
}


Python3
# Python program for the above approach
 
 
# Function to find the rank of a string
def findRank(s):
   
    # Store the length of the string
    N = len(s);
 
    # Stores its equivalent decimal value
    sb = "";
 
    # Traverse the string
    for i in range(0,N):
 
        if (s[i] == '0'):
            sb += str('0');
        else:
            sb += str('1');
 
    bin = str(sb);
 
    # Store the number of strings of
    # length less than N occurring
    # before the given string
    X = pow(2, N);
 
    # Store the decimal equivalent
    # number of string bin
    Y = int(bin);
 
    # Store the rank in answer
    ans = X + Y - 1;
 
    # Prthe answer
    print(ans);
 
# Driver Code
if __name__ == '__main__':
    S = "001";
    findRank(S);
 
# This code is contributed by shikhasingrajput


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
   
// Function to find the rank of a String
static void findRank(string s)
{
 
  // Store the length of the String
  int N = s.Length;
 
  // Stores its equivalent decimal value
  string bin = "";
 
  // Traverse the String
  for (int i = 0; i < N; i++)
  {
    if (s[i] == '0')
      bin += "0";
    else
      bin += "1";
  }
   
  // Store the number of string s of
  // length less than N occurring
  // before the given String
  int X = 1<= 0; i--)
  {
    if (bin[i] == '1') 
      res += (val);
 
    val *= 2;
  }
  int Y = res;
 
  // Store the rank in answer
  int ans = X + Y - 1;
 
  // Print the answer
  Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
   string S = "001";
    findRank(S);
}
}
 
// This code is contributed by splevel62.


Javascript


输出:
8

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

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