📌  相关文章
📜  a字符串中按字典顺序排列的第 K 个最小字符的频率

📅  最后修改于: 2021-09-06 06:38:10             🧑  作者: Mango

给定一个长度为N的字符串S和一个整数K ,任务是找到给定字符串出现的字典序K最小字符的频率。

例子:

方法:这个想法是按其 ASCII 值的升序对字符串进行排序。现在,在排序后的字符串找到第(K – 1)字符并找到它的频率。请按照以下步骤解决问题:

  • 按升序对给定的字符串S进行排序。
  • 将第(K – 1)字符在变量ch 中,即S[K – 1]
  • 找出S 中字符ch的频率并打印该值。

下面是该方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the frequency of
// the lexicographically Kth smallest
// character
void KthCharacter(
  string S, int N, int K)
{
 
  // Convert the string to
  // array of characters
  char strarray[N + 1];
  strcpy(strarray, S.c_str());
 
  // Sort the array in ascending order
  sort(strarray, strarray + N);
 
  // Store the Kth character
  char ch = strarray[K - 1];
 
  // Store the frequency of
  // the K-th character
  int count = 0;
 
  // Count the frequency of
  // the K-th character
  for (auto c : strarray)
  {
    if (c == ch)
      count++;
  }
 
  // Print the count
  cout << count;
}
 
// Driver Code
int main()
{
  string S = "geeksforgeeks";
  int N = S.length();
  int K = 3;
 
  KthCharacter(S, N, K);
 
  return 0;
}
 
// This code is contributed by sanjoy_62.


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the frequency of
    // the lexicographically Kth smallest
    // character
    public static void KthCharacter(
        String S, int N, int K)
    {
        // Convert the string to
        // array of characters
        char strarray[] = S.toCharArray();
 
        // Sort the array in ascending order
        Arrays.sort(strarray);
 
        // Store the Kth character
        char ch = strarray[K - 1];
 
        // Store the frequency of
        // the K-th character
        int count = 0;
 
        // Count the frequency of
        // the K-th character
        for (char c : strarray) {
            if (c == ch)
                count++;
        }
 
        // Print the count
        System.out.println(count);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "geeksforgeeks";
        int N = S.length();
        int K = 3;
 
        KthCharacter(S, N, K);
    }
}


Python3
# Python program for the above approach
 
# Function to find the frequency of
# the lexicographically Kth smallest
# character
def KthCharacter(S, N, K):
   
    # Convert the string to
    # array of characters
    strarray = [char for char in S];
 
    # Sort the array in ascending order
    strarray.sort();
 
    # Store the Kth character
    ch = strarray[K - 1];
 
    # Store the frequency of
    # the K-th character
    count = 0;
 
    # Count the frequency of
    # the K-th character
    for c in strarray:
        if (c == ch):
            count += 1;
 
    # Prthe count
    print(count);
 
# Driver Code
if __name__ == '__main__':
    S = "geeksforgeeks";
    N = len(S);
    K = 3;
 
    KthCharacter(S, N, K);
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the frequency of
  // the lexicographically Kth smallest
  // character
  public static void KthCharacter(
    string S, int N, int K)
  {
 
    // Convert the string to
    // array of characters
    char[] strarray = S.ToCharArray();
 
    // Sort the array in ascending order
    Array.Sort(strarray);
 
    // Store the Kth character
    char ch = strarray[K - 1];
 
    // Store the frequency of
    // the K-th character
    int count = 0;
 
    // Count the frequency of
    // the K-th character
    foreach (char c in strarray)
    {
      if (c == ch)
        count++;
    }
 
    // Print the count
    Console.Write(count);
  }
 
  // Driver Code
  public static void Main()
  {
    string S = "geeksforgeeks";
    int N = S.Length;
    int K = 3;
 
    KthCharacter(S, N, K);
  }
}
 
// This code is contributed by splevel62.


Javascript


输出:
4

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

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