📌  相关文章
📜  按升序对字符串数组进行排序,每个字符串按降序排序

📅  最后修改于: 2021-04-23 05:54:25             🧑  作者: Mango

给出的字符串S []尺寸N(1≤N≤10 5),每个字符串的排序字符的数组以降序,然后打印字符串的阵列中按升序排列。

例子:

方法:请按照以下步骤解决问题:

  • 遍历数组。
  • 按降序对每个字符串进行排序。qc2
  • 对每个字符串排序后,按升序对字符串数组进行排序。
  • 打印排序后的字符串数组。

下面是上述方法的实现。

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to sort the strings in
// descending order and sort the
// array in ascending order
void sortStr(string s[], int N)
{
 
    // Traverse the array of strings
    for (int i = 0; i < N; i++) {
 
        // Sort each string in descending order
        sort(s[i].begin(), s[i].end(),
             greater());
    }
 
    // Sort the array in ascending order
    sort(s, s + N);
 
    // Print the array of strings
    for (int i = 0; i < N; i++) {
        cout << s[i] << " ";
    }
}
 
// Driver Code
int main()
{
    string s[] = { "apple", "box", "cat" };
    int N = sizeof(s) / sizeof(s[0]);
    sortStr(s, N);
 
    return 0;
}


Java
// Java program of the above approach
import java.util.Arrays;
class GFG{
 
  // Function to sort the Strings in
  // descending order and sort the
  // array in ascending order
  static void sortStr(String s[], int N)
  {
 
    // Traverse the array of Strings
    for (int i = 0; i < N; i++)
    {
 
      // Sort each String in descending order       
      s[i] = reverse(sortString(s[i]));
    }
 
    // Sort the array in ascending order
    Arrays.sort(s);
 
    // Print the array of Strings
    for (int i = 0; i < N; i++)
    {
      System.out.print(s[i]+ " ");
    }
  }
  static String sortString(String inputString)
  {
 
    // convert input string to char array
    char tempArray[] = inputString.toCharArray();
 
    // sort tempArray
    Arrays.sort(tempArray);
 
    // return new sorted string
    return new String(tempArray);
  }
  static String reverse(String input)
  {
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--)
    {
      char temp = a[l];
      a[l] = a[r];
      a[r] = temp;
    }
    return String.valueOf(a);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String s[] = { "apple", "box", "cat" };
    int N = s.length;
    sortStr(s, N);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program of the above approach
 
# Function to sort the Strings in
# descending order and sort the
# array in ascending order
def sortStr(s, N):
   
    # Traverse the array of Strings
    for i in range(N):
       
        # Sort each String in descending order
        s[i] = "".join(reversed("".join(sorted(s[i])))) ;
         
    # Sort the array in ascending order
    s = " ".join(sorted(s))
 
    # Prthe array of Strings
    print(s)
 
# Driver Code
if __name__ == '__main__':
    s = ["apple", "box", "cat"];
    N = len(s);
    sortStr(s, N);
 
    # This code is contributed by shikhasingrajput


C#
// C# program of the above approach
using System;
class GFG {
 
  static void reverse(char[] a)
  {
    int i, n = a.Length;
    char t;
    for (i = 0; i < n / 2; i++)
    {
      t = a[i];
      a[i] = a[n - i - 1];
      a[n - i - 1] = t;
    }
  }
 
  // Function to sort the strings in
  // descending order and sort the
  // array in ascending order
  static void sortStr(string[] s, int N)
  {
 
    // Traverse the array of strings
    for (int i = 0; i < N; i++)
    {
      char[] t = s[i].ToCharArray();
 
      // Sort each string
      Array.Sort(t);
 
      // Reverse the string
      reverse(t);
      s[i] = String.Join("", t);
    }
 
    // Sort the array in ascending order
    Array.Sort(s);
 
    // Print the array of strings
    for (int i = 0; i < N; i++)
    {
      Console.Write(s[i] + " ");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    string[] s = { "apple", "box", "cat" };
    int N = s.Length;
    sortStr(s, N);
  }
}
 
// This code is contributed by subhammahato348


输出:
pplea tca xob

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