📌  相关文章
📜  用于检查给定字符串是否可以由其他两个字符串或其排列组成的Java程序

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

用于检查给定字符串是否可以由其他两个字符串或其排列组成的Java程序

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

例子:

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


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



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

下面是上述方法的实现:

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


请参阅有关检查给定字符串是否可以由其他两个字符串或其排列组成的完整文章以获取更多详细信息!