Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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

Method 1: In this approach, we begin by sorting the given string, then we run two nested loops to select two strings from the given array at a time and concatenate them. Then we sort the resultant string which we got after concatenation.
We then compare this string with the given string and check if they are equal or not. If found equal we return true.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
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<string> 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<string> v{ "fds", "oxq", "zoa", "epw", "amn" };
 
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Output

Yes

Time complexity: O(nlogn+m2klogk) 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(n) as tmp string has been created. Here n is maximum possible length of tmp string .

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: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
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<string> 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<string> v{ "fds", "oxq", "zoa", "epw", "amn" };
 
    if (isPossible(v, str))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Output

Yes

Time complexity: O(n+m2k) 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).

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

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



Last Updated : 23 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads