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

📅  最后修改于: 2021-04-17 15:28:22             🧑  作者: Mango

给定一个长度为N且整数为K的字符串S ,任务是查找在给定字符串,按词典上的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);
    }
}


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.


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


输出:
4

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