
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Check if a given string can be formed by two other strings or their permutations
A string is a continuous stream of characters, numbers. It may even contain special characters. An string can be composed of multiple sub-strings, also referred by words.
A permutation of a string is another string composed of the same set of characters, possibly arranged in a different order. It can basically be considered as a reordering of the string letters. For instance, abcd and badc are the permutations of the same string.
Sample Examples
Example 1 : str : “Permutation”
arr : {“repuio”,”mat”, “mtatn”,”mutate”}
Output :Yes
The input string “Permutation” can be formed from the pair strings at index 0 and 2, that is “repuio” and “mtatn” which can be rearranged to form the equivalent string.
This problem can be solved by using the following two methods in C++ −
Using sorting
Using counting sort
Method 1: Using sorting
An input array of strings is provided. During each nested iteration of the string using for loops, a pair of strings is captured from the input array. The sorted version of the input string as well as the generated pair string is compared if its permutations evaluate to the same string.
Syntax
sort()
The sort() method in C++ is used to sort the given string ascending or descending orderely.
sort(str.begin() , str.end())
Parameters
str.begin() - The starting of the string.
str.end() - The end of the string.
Algorithm
Step 1 − An input string and an array of strings is accepted.
Step 2 − The string is initially sorted using the sort() method in C++.
Step 3 − A nested loop iteration of the string is performed, wherein pairs of strings are accessed at a single time, using the pointers i and j. Each time a loop for j begins at the index i and goes on until the length of the string. Both the strings at indices i and j are accessed together and concatenated.
Step 4 − The concatenated string is then sorted again using the sort() method.
Step 5 − The concatenated string and the input sorted string are then compared. If they are equivalent, true is returned. Else, if none of the pair matches, false is returned.
Example
The following C++ code snippet is used to take as input a sample string and an array of strings and find out if the sample string can be generated using any pair of strings from the array −
//including the required libraries #include <bits/stdc++.h> using namespace std; //check if string can be formed by permutations of two strings bool permutation( string str,vector<string> arr) { int size = arr.size(); //sorting the given string sort(str.begin(), str.end()); //extracting two strings at a time for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { //concatenate the pair of string string concat = arr[i] + arr[j]; // Sort the resultant string sort(concat.begin(), concat.end()); //comparing if the order of characters in both strings are same if (concat.compare(str) == 0) { //if equal return true; } } } //no such combination exists return false; } int main(){ //declaring the input string string str = "tutorials"; //array of strings vector<string> arr{ "trils", "outa", "ginc", "abc" }; bool res = permutation(str,arr); if(res) cout << "Yes, the string can be formed by permutation of two other strings"; else cout << "No, the string can't be formed by permutation of two other strings"; return 0; }
Output
Yes, the string can be formed by permutation of two other strings
Method 2: Using counting sort
An input array of strings and a sample string is provided. The sample string is sorted using count sort. During each nested iteration of the string using for loops, a pair of strings is captured from the input array. The concatenated string, then generated, is also sorted using counting sort. The string as well as the generated pair string is compared if its permutations evaluate to the same string.
Counting Sort
An array of size 26 characters can be maintained to store the frequency of occurrence of each character in the string. This is eventually followed by extracting the characters in increasing order in order to obtain a sorted string.
Algorithm
Step 1 − An input string and an array of strings is accepted.
Step 2 − The string is initially sorted using the counting sort algorithm.
Step 3 − A nested loop iteration of the string is performed, wherein pairs of strings are accessed at a single time, using the pointers i and j.
Step 4 − Each time a loop for j begins at the index i and goes on until the length of the string. Both the strings at indices i and j are accessed together and concatenated.
Step 5 − The concatenated string is then sorted again using the counting sort mechanism.
Step 6 − The concatenated string and the input sorted string are then compared. If they are equivalent, true is returned. Else, if none of the pair matches, false is returned.
Example
//including the required libraries #include <bits/stdc++.h> using namespace std; // Function to sort the given string // using counting sort void countsort(string& str){ //declaring the numbr of characters int numchar = 26; //string length int len = str.length(); //initialise the count of array int count[numchar] = { 0 }; //declaring an index param int idx = 0; //traversing the string for (int i = 0; i < len; i++) { char ch = str[i] ; count[ch - 'a']++; } //increasing order insertion for (int i = 0; i < numchar; i++) { int j = 0; for (j=0; j < count[i];j++) { str[idx] = i + 'a'; idx++; } } } //check if string can be formed by permutations of two strings bool permutation( string str,vector<string> arr){ int size = arr.size(); //sorting the given string countsort(str); //extracting two strings at a time for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size; j++) { //concatenate the pair of string string concat = arr[i] + arr[j]; // Sort the resultant string countsort(concat); //comparing if the order of characters in both strings are same if (concat.compare(str) == 0) { //if equal return true; } } } //no such combination exists return false; } int main(){ //declaring the input string string str = "tutorials"; //array of strings vector<string> arr{ "trils", "outa", "ginc", "abc" }; printf("\nString 2:\n"); for (int i = 0; i <4 ; i++){ cout<<arr[i]<<endl; } bool res = permutation(str,arr); if(res) cout << "Yes, the string can be formed by permutation of two other strings"; else cout << "No, the string can't be formed by permutation of two other strings"; return 0; }
Output
String 2: trils outa ginc abc Yes, the string can be formed by permutation of two other strings
Conclusion
Since, the counting sort mechanism takes into role only the evaluation of the character array of a finite size, that is 26, the second approach is considered to be better than the first one. The second approach reduces the time complexity of a polynomial time approach to a nearly polynomial approximation.
- Related Articles
- Program to check whether final string can be formed using other two strings or not in Python
- Check if given string can be formed by concatenating string elements of list in Python
- Check if a string can be formed from another string using given constraints in Python
- Check if given string can be split into four distinct strings in Python
- All possible strings of any length that can be formed from a given string?
- Check whether given string can be generated after concatenating given strings in Python
- Check if a two-character string can be made using given words in Python
- Program to check two strings can be equal by swapping characters or not in Python
- JavaScript Program to Check if a string can be formed from another string by at most X circular clockwise shifts
- Write a program in C++ to check if a string can be obtained by rotating another string by two places
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- Write a program in JavaScript to check if two strings are anagrams of each other or not
- Check if a string can become empty by recursively deleting a given sub-string in Python
- Check if a string can become empty by recursively deleting a given sub-string in C++
