📌  相关文章
📜  根据不同字符的数量对字符串数组进行排序

📅  最后修改于: 2021-05-17 04:43:53             🧑  作者: Mango

给定一个字符串数组arr []作为输入,任务是打印单词中的单词,这些单词按单词中出现的不同字符的数量排序,然后是单词的长度

笔记:

  • 如果两个单词具有相同数量的不同字符,则总字符更多的单词排在第一位。
  • 如果两个单词具有相同数量的不同字符且长度相同,则必须首先打印句子中较早出现的单词。

例子:

方法:这个想法是用 排序

  • 初始化地图数据结构,以计算给定数组的每个字符串中所有可能的不同字符。
  • 然后通过传递比较器函数对数组进行排序,其中排序是通过单词中唯一字符的数量和单词长度进行的。
  • 排序完成后,打印数组的字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return no of
// unique character in a word
int countDistinct(string s)
{
    // Initialize map
    unordered_map m;
 
    for (int i = 0; i < s.length(); i++) {
        // Count distinct characters
        m[s[i]]++;
    }
 
    return m.size();
}
 
// Function to perform sorting
bool compare(string& s1, string& s2)
{
    if (countDistinct(s1) == countDistinct(s2)) {
        // Check if size of string 1
        // is same as string 2 then
        // return false because s1 should
        // not be placed before s2
        if (s1.size() == s2.size()) {
            return false;
        }
        return s1.size() > s2.size();
    }
    return countDistinct(s1) < countDistinct(s2);
}
 
// Function to print the sorted array of string
void printArraystring(string str[], int n)
{
    for (int i = 0; i < n; i++)
        cout << str[i] << " ";
}
 
// Driver Code
int main()
{
    string arr[] = { "Bananas", "do",
                     "not", "grow", "in",
                     "Mississippi" };
    int n = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function call
    sort(arr, arr + n, compare);
 
    // Print result
    printArraystring(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to return no of
// unique character in a word
static int countDistinct(String s)
{
     
    // Initialize map
    Map m = new HashMap<>();
 
    for(int i = 0; i < s.length(); i++)
    {
         
        // Count distinct characters
        if (m.containsKey(s.charAt(i)))
        {
            m.put(s.charAt(i),
            m.get(s.charAt(i)) + 1);
        }
        else
        {
            m.put(s.charAt(i), 1);
        }
    }
    return m.size();
}
 
// Function to print the sorted
// array of string
static void printArraystring(String[] str,
                             int n)
{
    for(int i = 0; i < n; i++)
    {
        System.out.print(str[i] + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    String[] arr = { "Bananas", "do",
                     "not", "grow",
                     "in", "Mississippi" };
    int n = arr.length;
 
    // Function call
    Arrays.sort(arr, new Comparator()
    {
        public int compare(String a, String b)
        {
            if (countDistinct(a) ==
                countDistinct(b))
            {
                 
                // Check if size of string 1
                // is same as string 2 then
                // return false because s1 should
                // not be placed before s2
                return (b.length() - a.length());
            }
            else
            {
                return (countDistinct(a) -
                        countDistinct(b));
            }
        }
    });
 
    // Print result
    printArraystring(arr, n);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program of the above approach
import functools
 
# Function to return no of
# unique character in a word
def countDistinct(s):
 
    # Initialize dictionary
    m = {}
 
    for i in range(len(s)):
 
        # Count distinct characters
        if s[i] not in m:
            m[s[i]] = 1
        else:
            m[s[i]] += 1
 
    return len(m)
 
# Function to perform sorting
def compare(a, b):
     
    if (countDistinct(a) == countDistinct(b)):
         
        # Check if size of string 1
        # is same as string 2 then
        # return false because s1 should
        # not be placed before s2
        return (len(b) - len(a))
    else:
        return (countDistinct(a) - countDistinct(b))
 
# Driver Code
arr = [ "Bananas", "do", "not",
        "grow", "in","Mississippi" ]
 
n = len(arr)
 
# Print result
print(*sorted(
    arr, key = functools.cmp_to_key(compare)), sep = ' ')
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program of the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
     
// Function to return no of
// unique character in a word
static int countDistinct(string s)
{
 
    // Initialize map
    Dictionary m = new Dictionary();
 
    for(int i = 0; i < s.Length; i++)
    {
         
        // Count distinct characters
        if (m.ContainsKey(s[i]))
        {
            m[s[i]]++;
        }
        else
        {
            m[s[i]] = 1;
        }
    }
    return m.Count;
}
 
static int compare(string s1, string s2)
{
    if (countDistinct(s1) == countDistinct(s2))
    {
         
        // Check if size of string 1
        // is same as string 2 then
        // return false because s1 should
        // not be placed before s2
        return s2.Length - s1.Length;
    }
    else
    {
        return (countDistinct(s1) -
                countDistinct(s2));
    }
}
 
// Function to print the sorted array of string
static void printArraystring(string []str, int n)
{
    for(int i = 0; i < n; i++)
    {
        Console.Write(str[i] + " ");
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    string []arr = { "Bananas", "do",
                     "not", "grow",
                     "in", "Mississippi" };
    int n = arr.Length;
     
    // Function call
    Array.Sort(arr, compare);
     
    // Print result
    printArraystring(arr, n);
}
}
 
// This code is contributed by rutvik_56


输出:
do in not Mississippi Bananas grow

时间复杂度: O(n * log n)

辅助空间: O(n)