📌  相关文章
📜  检查字符串S 的任何字谜是否在字典上小于字符串T

📅  最后修改于: 2022-05-13 01:56:08.006000             🧑  作者: Mango

检查字符串S 的任何字谜是否在字典上小于字符串T

给定两个字符串ST ,任务是 检查字符串S的任何字谜是否在字典上小于字符串T的任何字谜。

例子:

方法:该方法是检查字符串S 的字典序上最小的字谜是否小于字符串T 的字典序上最大的字谜。如果是,则答案是Yes 。否则,。现在,请按照以下步骤解决此问题:

  1. 对字符串S进行排序以获得其字典上最小的字谜。
  2. 对字符串T进行反向排序以获得其字典序上最大的字谜。
  3. 检查新字符串T是否大于新字符串S。如果是,请打印Yes 。否则,打印No

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if any anagram
// of string S is lexicographically
// smaller than any anagram of string T
void CompareAnagrams(string S, string T)
{
    // Sort string S
    sort(S.begin(), S.end());
 
    // Reverse sort string T
    sort(T.begin(), T.end(), greater());
 
 
    // Comparing both the strings
    if (S.compare(T) < 0) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}
 
// Driver code
int main()
{
    string S = "cd";
    string T = "abc";
 
    CompareAnagrams(S, T);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
   
    // function to Reverse String
    static String ReverseString(String myStr)
    {
        String nstr = "";
        char ch;
 
        for (int i = 0; i < myStr.length(); i++) {
            ch = myStr.charAt(i); // extracts each character
            nstr
                = ch + nstr; // adds each character in
                             // front of the existing string
        }
 
        return nstr;
    }
   
    // function to print string in sorted order
    static String sortString(String str)
    {
        char[] arr = str.toCharArray();
        Arrays.sort(arr);
        return new String(arr);
    }
 
    // Function to check if any anagram
    // of string S is lexicographically
    // smaller than any anagram of string T
    static void CompareAnagrams(String S, String T)
    {
 
        // Sort string S
        sortString(S);
 
        // Reverse sort string T
        T = sortString(T);
        T = ReverseString(T);
 
        // Comparing both the strings
        if (S.compareTo(T) < 0) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "cd";
        String T = "abc";
 
        CompareAnagrams(S, T);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to check if any anagram
# of string S is lexicographically
# smaller than any anagram of string T
 
 
def CompareAnagrams(S,  T):
 
    # Sort string S
    S = list(S)
    S.sort()
    S = ''.join(S)
 
    # Reverse sort string T
    T = list(T)
    T.sort(reverse=True)
    T = ''.join(T)
 
    # Comparing both the strings
    if (S < T):
        print("Yes")
 
    else:
        print("No")
 
# Driver code
if __name__ == "__main__":
 
    S = "cd"
    T = "abc"
 
    CompareAnagrams(S, T)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
public class GFG
{
   
// function to Reverse String
static string ReverseString(string myStr)
    {
        char[] myArr = myStr.ToCharArray();
        Array.Reverse(myArr);
        return new string(myArr);
    }
 
// function to print string in sorted order
    static void sortString(String str) {
        char []arr = str.ToCharArray();
        Array.Sort(arr);
        String.Join("",arr);
    }
 
// Function to check if any anagram
// of string S is lexicographically
// smaller than any anagram of string T
static void CompareAnagrams(string S, string T)
{
   
    // Sort string S
    sortString(S);
 
    // Reverse sort string T
    sortString(T);
    ReverseString(T);
     
 
    // Comparing both the strings
    if (string.Compare(S, T) < 0) {
        Console.WriteLine("Yes");
    }
    else {
        Console.WriteLine("No");
    }
}
 
// Driver Code
public static void Main(String []args) {
     
    string S = "cd";
    string T = "abc";
 
    CompareAnagrams(S, T);
}
}
 
// This code is contributed by target_2.


Javascript



输出
No

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