GeeksforGeeks App
Open App
Browser
Continue

# Java Program for Check if given string can be formed by two other strings or their permutations

Given a string str and an array of strings arr[], the task is to check if the given string can be formed by any of the string pair from the array or their permutations. Examples:

Input: str = “amazon”, arr[] = {“loa”, “azo”, “ft”, “amn”, “lka”} Output: Yes The chosen strings are “amn” and “azo” which can be rearranged as “amazon”. Input: str = “geeksforgeeks”, arr[] = {“geeks”, “geek”, “for”} Output: No

## 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`

Time complexity: O(n*logn + m2*k*logk) where n is the size of the given string, m is the size of the given array, and k is the maximum size obtained by adding any two strings (which is basically the sum of the size of the two longest strings in the given array).

Space Complexity: O(x) where x is the maximum length of two input strings after concatenating

Method 2: Counting sort can be used to reduce the running time of the above approach. Counting sort uses a table to store the count of each character. We have 26 alphabets hence we make an array of size 26 to store counts of each character in the string. Then take the characters in increasing order to get the sorted string. Below is the implementation of the above approach:

## 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`

Time complexity: O(n+(m^2)*k) where n is the size of the given string, m is the size of the given array, and k is the maximum size obtained by adding any two strings

Please refer complete article on Check if given string can be formed by two other strings or their permutations for more details!

My Personal Notes arrow_drop_up