📌  相关文章
📜  检查给定的字符串可以由其他两个字符串或它们的排列组成

📅  最后修改于: 2021-04-23 17:37:16             🧑  作者: Mango

给定一个字符串str和一个字符串arr []数组,任务是检查给定的字符串可以由数组中的任何字符串对或其排列组成。

例子:

方法1:我们首先对给定的字符串排序,然后运行两个嵌套循环,并一次从给定的数组中选择两个字符串,然后将它们连接起来,然后对结果字符串进行排序。
排序后,我们检查它是否等于给定的已排序字符串。
因此,总体时间复杂度将为O(n * mlogm),其中n是给定数组的大小,m是字符串的长度(最大)。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
bool isPossible(vector v, string str)
{
  
    // Sort the given string
    sort(str.begin(), str.end());
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
  
            // Get the concatenated string
            string temp = v[i] + v[j];
  
            // Sort the resultant string
            sort(temp.begin(), temp.end());
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compare(str) == 0) {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Driver code
int main()
{
    string str = "amazon";
    vector v{ "fds", "oxq", "zoa", "epw", "amn" };
  
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
static boolean isPossible(Vector v, String str)
{
  
    // Sort the given string
    str = sortString(str);
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) 
    {
        for (int j = i + 1; j < v.size(); j++)
        {
  
            // Get the concatenated string
            String temp = v.get(i) + v.get(j);
  
            // Sort the resultant string
            temp = sortString(temp);
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compareTo(str) == 0) 
            {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Method to sort a string alphabetically 
public 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) 
{
    String str = "amazon";
    String []arr = { "fds", "oxq", "zoa", "epw", "amn" };
    Vector v = new Vector(Arrays.asList(arr));
  
    if (isPossible(v, str))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function that returns true if str can be 
# generated from any permutation of the 
# two strings selected from the given vector 
def isPossible(v, string ) : 
      
    char_list = list(string)
      
    # Sort the given string
    char_list.sort()
      
    # Select two strings at a time from given vector
    for i in range(len(v)-1) :
        for j in range(len(v)) :
              
            # Get the concatenated string
            temp = v[i] + v[j];
              
            # Sort the resultant string 
            temp_list = list(temp)
            temp_list.sort()
              
            # If the resultant string is equal
            # to the given string str
            if (temp_list == char_list) :
                return True;
                  
    # No valid pair found
    return False; 
  
# Driver code 
if __name__ == "__main__" : 
  
    string = "amazon"; 
    v = [ "fds", "oxq", "zoa", "epw", "amn" ]; 
  
    if (isPossible(v, string)):
        print("Yes"); 
    else :
        print("No"); 
          
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
class GFG 
{
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
static Boolean isPossible(List v, String str)
{
  
    // Sort the given string
    str = sortString(str);
  
    // Select two strings at a time from given vector
    for (int i = 0; i  v = new List(arr);
  
    if (isPossible(v, str))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by Princi Singh


C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAX 26
  
// Function to sort the given string
// using counting sort
void countingsort(string& s)
{
    // Array to store the count of each character
    int count[MAX] = { 0 };
    for (int i = 0; i < s.length(); i++) {
        count[s[i] - 'a']++;
    }
    int index = 0;
  
    // Insert characters in the string
    // in increasing order
    for (int i = 0; i < MAX; i++) {
        int j = 0;
        while (j < count[i]) {
            s[index++] = i + 'a';
            j++;
        }
    }
}
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
bool isPossible(vector v, string str)
{
  
    // Sort the given string
    countingsort(str);
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
  
            // Get the concatenated string
            string temp = v[i] + v[j];
  
            // Sort the resultant string
            countingsort(temp);
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compare(str) == 0) {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Driver code
int main()
{
    string str = "amazon";
    vector v{ "fds", "oxq", "zoa", "epw", "amn" };
  
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
  
class GFG 
{
static int MAX = 26; 
  
// Function to sort the given string 
// using counting sort 
static String countingsort(char[] s) 
{ 
      
    // Array to store the count of each character 
    int []count = new int[MAX]; 
    for (int i = 0; i < s.length; i++) 
    { 
        count[s[i] - 'a']++; 
    } 
    int index = 0; 
  
    // Insert characters in the string 
    // in increasing order 
    for (int i = 0; i < MAX; i++)
    { 
        int j = 0; 
        while (j < count[i]) 
        { 
            s[index++] = (char)(i + 'a'); 
            j++; 
        } 
    } 
        return String.valueOf(s);
} 
  
// Function that returns true if str can be 
// generated from any permutation of the 
// two strings selected from the given vector 
static boolean isPossible(Vector v, 
                                 String str) 
{ 
  
    // Sort the given string 
    str=countingsort(str.toCharArray()); 
  
    // Select two strings at a time from given vector 
    for (int i = 0; i < v.size() - 1; i++) 
    { 
        for (int j = i + 1; j < v.size(); j++) 
        { 
  
            // Get the concatenated string 
            String temp = v.get(i) + v.get(j); 
  
            // Sort the resultant string 
            temp = countingsort(temp.toCharArray()); 
  
            // If the resultant string is equal 
            // to the given string str 
            if (temp.equals(str)) 
            { 
                return true; 
            } 
        } 
    } 
  
    // No valid pair found 
    return false; 
} 
  
// Driver code 
public static void main(String[] args) 
{
    String str = "amazon"; 
    String []arr = { "fds", "oxq", "zoa", "epw", "amn" };
    Vector v = new Vector(Arrays.asList(arr));
  
    if (isPossible(v, str)) 
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
MAX = 26
  
# Function to sort the given string
# using counting sort
def countingsort(s):
    # Array to store the count of each character
    count = [0 for i in range(MAX)]
    for i in range(len(s)):
        count[ord(s[i]) - ord('a')] += 1
    index = 0
  
    # Insert characters in the string
    # in increasing order
      
    for i in range(MAX):
        j = 0
        while (j < count[i]):
            s = s.replace(s[index],chr(97+i))
            index += 1
            j += 1
          
  
# Function that returns true if str can be
# generated from any permutation of the
# two strings selected from the given vector
def isPossible(v, str1):
    # Sort the given string
    countingsort(str1);
  
    # Select two strings at a time from given vector
    for i in range(len(v)-1):
        for j in range(i + 1,len(v),1):
            # Get the concatenated string
            temp = v[i] + v[j]
  
            # Sort the resultant string
            countingsort(temp)
  
            # If the resultant string is equal
            # to the given string str
            if (temp == str1):
                return False
              
    # No valid pair found
    return True
  
# Driver code
if __name__ == '__main__':
    str1 = "amazon"
    v = ["fds", "oxq", "zoa", "epw", "amn"]
  
    if (isPossible(v, str1)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the above approach 
using System;
using System.Collections.Generic;
      
class GFG 
{
static int MAX = 26; 
  
// Function to sort the given string 
// using counting sort 
static String countingsort(char[] s) 
{ 
      
    // Array to store the count of each character 
    int []count = new int[MAX]; 
    for (int i = 0; i < s.Length; i++) 
    { 
        count[s[i] - 'a']++; 
    } 
      
    int index = 0; 
  
    // Insert characters in the string 
    // in increasing order 
    for (int i = 0; i < MAX; i++)
    { 
        int j = 0; 
        while (j < count[i]) 
        { 
            s[index++] = (char)(i + 'a'); 
            j++; 
        } 
    } 
        return String.Join("", s);
} 
  
// Function that returns true if str can be 
// generated from any permutation of the 
// two strings selected from the given vector 
static Boolean isPossible(List v, 
                               String str) 
{ 
  
    // Sort the given string 
    str = countingsort(str.ToCharArray()); 
  
    // Select two strings at a time from given vector 
    for (int i = 0; i < v.Count - 1; i++) 
    { 
        for (int j = i + 1; j < v.Count; j++) 
        { 
  
            // Get the concatenated string 
            String temp = v[i] + v[j]; 
  
            // Sort the resultant string 
            temp = countingsort(temp.ToCharArray()); 
  
            // If the resultant string is equal 
            // to the given string str 
            if (temp.Equals(str)) 
            { 
                return true; 
            } 
        } 
    } 
  
    // No valid pair found 
    return false; 
} 
  
// Driver code 
public static void Main(String[] args) 
{
    String str = "amazon"; 
    String []arr = { "fds", "oxq", 
                     "zoa", "epw", "amn" };
    List v = new List(arr);
  
    if (isPossible(v, str)) 
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by PrinciRaj1992


输出:
Yes

方法2:计数排序可用于减少上述方法的运行时间。计数排序使用表存储每个字符的计数。我们有26个字母,因此我们制作了一个大小为26的数组,以存储字符串中每个字符的计数。然后以递增的顺序获取字符,以获得排序后的字符串。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
#define MAX 26
  
// Function to sort the given string
// using counting sort
void countingsort(string& s)
{
    // Array to store the count of each character
    int count[MAX] = { 0 };
    for (int i = 0; i < s.length(); i++) {
        count[s[i] - 'a']++;
    }
    int index = 0;
  
    // Insert characters in the string
    // in increasing order
    for (int i = 0; i < MAX; i++) {
        int j = 0;
        while (j < count[i]) {
            s[index++] = i + 'a';
            j++;
        }
    }
}
  
// Function that returns true if str can be
// generated from any permutation of the
// two strings selected from the given vector
bool isPossible(vector v, string str)
{
  
    // Sort the given string
    countingsort(str);
  
    // Select two strings at a time from given vector
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) {
  
            // Get the concatenated string
            string temp = v[i] + v[j];
  
            // Sort the resultant string
            countingsort(temp);
  
            // If the resultant string is equal
            // to the given string str
            if (temp.compare(str) == 0) {
                return true;
            }
        }
    }
  
    // No valid pair found
    return false;
}
  
// Driver code
int main()
{
    string str = "amazon";
    vector v{ "fds", "oxq", "zoa", "epw", "amn" };
  
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

Java

// Java implementation of the approach 
import java.util.*;
  
class GFG 
{
static int MAX = 26; 
  
// Function to sort the given string 
// using counting sort 
static String countingsort(char[] s) 
{ 
      
    // Array to store the count of each character 
    int []count = new int[MAX]; 
    for (int i = 0; i < s.length; i++) 
    { 
        count[s[i] - 'a']++; 
    } 
    int index = 0; 
  
    // Insert characters in the string 
    // in increasing order 
    for (int i = 0; i < MAX; i++)
    { 
        int j = 0; 
        while (j < count[i]) 
        { 
            s[index++] = (char)(i + 'a'); 
            j++; 
        } 
    } 
        return String.valueOf(s);
} 
  
// Function that returns true if str can be 
// generated from any permutation of the 
// two strings selected from the given vector 
static boolean isPossible(Vector v, 
                                 String str) 
{ 
  
    // Sort the given string 
    str=countingsort(str.toCharArray()); 
  
    // Select two strings at a time from given vector 
    for (int i = 0; i < v.size() - 1; i++) 
    { 
        for (int j = i + 1; j < v.size(); j++) 
        { 
  
            // Get the concatenated string 
            String temp = v.get(i) + v.get(j); 
  
            // Sort the resultant string 
            temp = countingsort(temp.toCharArray()); 
  
            // If the resultant string is equal 
            // to the given string str 
            if (temp.equals(str)) 
            { 
                return true; 
            } 
        } 
    } 
  
    // No valid pair found 
    return false; 
} 
  
// Driver code 
public static void main(String[] args) 
{
    String str = "amazon"; 
    String []arr = { "fds", "oxq", "zoa", "epw", "amn" };
    Vector v = new Vector(Arrays.asList(arr));
  
    if (isPossible(v, str)) 
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by 29AjayKumar

Python3

# Python 3 implementation of the approach
MAX = 26
  
# Function to sort the given string
# using counting sort
def countingsort(s):
    # Array to store the count of each character
    count = [0 for i in range(MAX)]
    for i in range(len(s)):
        count[ord(s[i]) - ord('a')] += 1
    index = 0
  
    # Insert characters in the string
    # in increasing order
      
    for i in range(MAX):
        j = 0
        while (j < count[i]):
            s = s.replace(s[index],chr(97+i))
            index += 1
            j += 1
          
  
# Function that returns true if str can be
# generated from any permutation of the
# two strings selected from the given vector
def isPossible(v, str1):
    # Sort the given string
    countingsort(str1);
  
    # Select two strings at a time from given vector
    for i in range(len(v)-1):
        for j in range(i + 1,len(v),1):
            # Get the concatenated string
            temp = v[i] + v[j]
  
            # Sort the resultant string
            countingsort(temp)
  
            # If the resultant string is equal
            # to the given string str
            if (temp == str1):
                return False
              
    # No valid pair found
    return True
  
# Driver code
if __name__ == '__main__':
    str1 = "amazon"
    v = ["fds", "oxq", "zoa", "epw", "amn"]
  
    if (isPossible(v, str1)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the above approach 
using System;
using System.Collections.Generic;
      
class GFG 
{
static int MAX = 26; 
  
// Function to sort the given string 
// using counting sort 
static String countingsort(char[] s) 
{ 
      
    // Array to store the count of each character 
    int []count = new int[MAX]; 
    for (int i = 0; i < s.Length; i++) 
    { 
        count[s[i] - 'a']++; 
    } 
      
    int index = 0; 
  
    // Insert characters in the string 
    // in increasing order 
    for (int i = 0; i < MAX; i++)
    { 
        int j = 0; 
        while (j < count[i]) 
        { 
            s[index++] = (char)(i + 'a'); 
            j++; 
        } 
    } 
        return String.Join("", s);
} 
  
// Function that returns true if str can be 
// generated from any permutation of the 
// two strings selected from the given vector 
static Boolean isPossible(List v, 
                               String str) 
{ 
  
    // Sort the given string 
    str = countingsort(str.ToCharArray()); 
  
    // Select two strings at a time from given vector 
    for (int i = 0; i < v.Count - 1; i++) 
    { 
        for (int j = i + 1; j < v.Count; j++) 
        { 
  
            // Get the concatenated string 
            String temp = v[i] + v[j]; 
  
            // Sort the resultant string 
            temp = countingsort(temp.ToCharArray()); 
  
            // If the resultant string is equal 
            // to the given string str 
            if (temp.Equals(str)) 
            { 
                return true; 
            } 
        } 
    } 
  
    // No valid pair found 
    return false; 
} 
  
// Driver code 
public static void Main(String[] args) 
{
    String str = "amazon"; 
    String []arr = { "fds", "oxq", 
                     "zoa", "epw", "amn" };
    List v = new List(arr);
  
    if (isPossible(v, str)) 
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by PrinciRaj1992 
输出:
Yes