📜  检查一个字符串的排列是否可以破坏另一个字符串的排列

📅  最后修改于: 2021-06-25 13:34:30             🧑  作者: Mango

鉴于两个字符串str1str2的,任务是检查给出的字符串str1str2中的任何排列是可能的,使得一个字符串的每个索引中的字符大于或等于另一个字符串。
例子:

天真的方法:这个想法是生成一个字符串的所有排列,并检查任何排列的每个字符是否大于另一个字符串,然后打印“是”,否则打印“否”。
时间复杂度: O(N ^ 2)
辅助空间: O(1)
高效的方法:由于我们必须检查一个字符串的排列的每个字符是否大于或等于另一个字符串的排列。想法是按照字母顺序对两个字符串进行排序。现在迭代所有字符串的字符一个循环,如果所有的字符串str1中的字符串小于str2的或字符串STR2的所有字符小于STR1然后打印打印别的没有
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if permutation of
// one string can break permutation of
// another string or not
bool CanBreak(string A, string B)
{
    // Sort both the strings
    // in alphabetical order
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
 
    bool ans1 = true, ans2 = true;
 
    // Iterate over the strings
    for (int i = 0; i < A.length(); i++) {
 
        // If any of the string can break
        // other then mark ans as false;
        if (A[i] < B[i])
            ans1 = false;
        if (B[i] < A[i])
            ans2 = false;
    }
 
    // If any of the string can break
    return ans1 || ans2;
}
 
// Driver Code
int main()
{
    // Given string A and B
    string A = "abc", B = "xya";
 
    // Function Call
    if (CanBreak(A, B))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if permutation of
// one String can break permutation of
// another String or not
static boolean CanBreak(String A, String B)
{
     
    // Sort both the Strings
    // in alphabetical order
    A = sortString(A);
    B = sortString(B);
 
    boolean ans1 = true, ans2 = true;
 
    // Iterate over the Strings
    for(int i = 0; i < A.length(); i++)
    {
         
        // If any of the String can break
        // other then mark ans as false;
        if (A.charAt(i) < B.charAt(i))
            ans1 = false;
        if (B.charAt(i) < A.charAt(i))
            ans2 = false;
    }
 
    // If any of the String can break
    return ans1 || ans2;
}
 
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);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String A and B
    String A = "abc", B = "xya";
 
    // Function call
    if (CanBreak(A, B))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for
# the above approach
 
# Function to check if
# permutation of one string
# can break permutation of
# another string or not
def CanBreak(A, B):
 
    # Sort both the strings
    # in alphabetical order
    A = "".join(sorted(A))
    B = "".join(sorted(B))
    ans1 = True
    ans2 = True
 
    # Iterate over the strings
    for i in range(len(A)):
 
        # If any of the string
        # can break other then
        # mark ans as false;
        if(A[i] < B[i]):
            ans1 = False
        if(B[i] < A[i]):
            ans2 = False
 
    # If any of the string
    # can break
    return (ans1 or ans2)
 
# Driver Code
 
# Given string A and B
A = "abc"
B = "xya"
 
# Function Call
if(CanBreak(A, B)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to check if permutation of
// one String can break permutation of
// another String or not
static bool CanBreak(String A, String B)
{
     
    // Sort both the Strings
    // in alphabetical order
    A = sortString(A);
    B = sortString(B);
 
    bool ans1 = true, ans2 = true;
 
    // Iterate over the Strings
    for(int i = 0; i < A.Length; i++)
    {
         
        // If any of the String can break
        // other then mark ans as false;
        if (A[i] < B[i])
            ans1 = false;
        if (B[i] < A[i])
            ans2 = false;
    }
 
    // If any of the String can break
    return ans1 || ans2;
}
 
static String sortString(String inputString)
{
     
    // Convert input string to char array
    char []tempArray = inputString.ToCharArray();
     
    // Sort tempArray
    Array.Sort(tempArray);
     
    // Return new sorted string
    return new String(tempArray);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String A and B
    String A = "abc", B = "xya";
 
    // Function call
    if (CanBreak(A, B))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
Yes

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