- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Transform a string such that it has abcd..z as a subsequence
Transforming a string aka string transformation is an operation in C++ by which the result stores in an output array after the execution of the whole process. In C++ there is a function called "transform()", present in the directory of the C++ environment by which we can transform a string into a new one.
There are two forms of this transform function −
Unary operation
Operation applies on the every element of the input array.
After the operation being done the result will store in an output array.
Binary operation
Operation applies on each element of a particular array.
The first input element and the second respective input element takes a part in the operation.
The output data will store in an output array.
A subsequence string is a completely new string, generated from the input string by performing various operations (like: deletion) on it. For a subsequence string the operations take place without tampering the remaining characters.
For a string transformation, the input contains the operated string with a length of n+1. Original character is belong to the series of a to z. The printed string's length is considered as n here, which is an output string here.
In this article, we will learn how to transform a string such that it has abcd….z as a subsequence by using a C++ environment.
Algorithm of a subsequent string by recursion
By using recursion approach, here is the possible algorithm for a subsequent string. Here’s is the particular string and T is the consumed time to make the operation done.
Step 1 − Count Occurrences.
Step 2 − If, i= length(s) and j= length(T).
Step 3− Then Return 1.
Step 4 − End.
Step 5 − If, i= length(S).
Step 6 − Then Return 0.
Step 7 − End.
Step 8 − Count <-- 0.
Step 9 − If, j<length (T) AND Si = Tj.
Step 10 − Count<-- count + countOccurences(i+1,j+1).
Step 11 − End.
Step 12 − Count<-- count + countOccurences(i+1,j).
Step 13 − Return count.
Step 14 − End.
Syntax of a subsequent array
Here, we have two given sequences. X and Y. Initialize a table with a dimension of X.length * Y.length X.label1 = X Y.label2 = Y CS1[0][] = 0 CS2[][0] = 0 Start from CS[1][1] Compare X[i] and Y[j] If X[i] = Y[j] CS[i][j] = 1 + CS[i-1, j-1] Point an arrow to CS[i][j] Else CS[i][j] = max(CS[i-1][j], CS[i][j-1]) Point an arrow to max(CS[i-1][j], CS[i][j-1])
Here we have created the basic working syntax of a subsequent array. When two sequences are there, we have to follow these steps to get the output.
Approaches to follow
Approach 1 − Transform a string by using C++
Approach 2 − Unary operation on a string by using C++
Approach 3 − Binary operation on a string by using C++
Approach 4 − Print all possible subsequent string by using C++
Approach 5 − Transform a string such that it has abcd….z as a subsequence using C++
Transform a string by using C++
In this C++ code, we have created a new string and remove all the vowels from the input string. In output # has been added at the place of those vowels.
Example 1
#include <bits/stdc++.h> using namespace std; string change_case(string r) { int l = r.length(); for(int i = 0 ; i < l ; i++) { if(r[i] >= 'a' && r[i] <= 'z') r[i] = r[i] - 32; else if(r[i] >= 'A' && r[i] <= 'Z') r[i] = r[i] + 32; } return r; } string delete_vowels(string a) { string temp = ""; int l = a.length(); for(int i = 0 ; i < l ; i++) { if(a[i] != 'a' && a[i] != 'e' && a[i] != 'i' && a[i] != 'o' && a[i] != 'u' && a[i] != 'A' && a[i] != 'E' && a[i] != 'O' && a[i] != 'U'&& a[i] != 'I') temp += a[i]; } return temp; } string insert_hash(string a) { string temp = ""; int l = a.length(); for(int i = 0 ; i < l ; i++) { if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) temp = temp + '#' + a[i]; else temp = temp + a[i]; } return temp; } void transformSting(string a) { string b = delete_vowels(a); string c = change_case(b); string d = insert_hash(c); if(d=="") cout<<"-1"<<endl; else cout << d<<endl; } int main() { string a = "RudraDevDas!!"; string b = "aeiou"; transformSting(a); transformSting(b); return 0; }
Output
#r#D#R#d#V#d#S!! -1
Unary operation on a string by using C++
In this particular code we have demonstrated how the unary operation works on an input array. The function takes a pointer at starting and ending for a single input. And after operation to the starting position of an output array.
Example 2
#include <iostream> #include <algorithm> using namespace std; int op_increment (int x) { x = x + 1; return x; } int main () { int n = 5; int input_array[] = {7, 16, 10, 97, 2001}; int output_array[n]; std::cout << "Input array present here:"; for(int i=0; i<5; i++){ cout << ' ' << input_array[i]; } cout << '\n'; transform (input_array, input_array+5, output_array, op_increment); std::cout << "The output array now contains with:"; for(int i=0; i<5; i++){ cout << ' ' << output_array[i]; } cout << '\n'; return 0; }
Output
Input array present here: 7 16 10 97 2001 The output array now contains with: 8 17 11 98 2002
Binary operation on a string by using C++
In this particular code we have demonstrated how the binary operation works on an input array. The function transform(), attached a pointer at the starting point and the first input array at the ending point. Remember that, the binary operation always works on two input data set.
Example 3
#include <iostream> #include <algorithm> #include <vector> using namespace std; int op_add (int i, int j) { return i+j; } int main () { int n = 5; int arr1[] = {7, 16, 10, 2001, 1997}; int arr2[] = {1, 2, 3, 4, 5}; int output[n]; std::cout << "Input data in array1:"; for(int i=0; i<n; i++){ cout << ' ' << arr1[i]; } cout << '\n'; std::cout << "Input data in array2:"; for(int i=0; i<n; i++){ cout << ' ' << arr2[i]; } cout << '\n'; std::transform (arr1, arr1+n, arr2, output, op_add); std::cout << "Output array is here now:"; for(int i=0; i<5; i++){ cout << ' ' << output[i]; } cout << '\n'; return 0; }
Output
Input data in array1: 7 16 10 2001 1997 Input data in array2: 1 2 3 4 5 Output array is here now: 8 18 13 2005 2002
Print all subdequent string by using C++
The pick and don’t pick concept are applied to find out all the subsequence of a particular array. Here in the process may be some character can be deleted without changing the order of the elements. Here, this process of the time complexity is O(2^n) and space complexity is O(n).
Example 4
#include <bits/stdc++.h> using namespace std; void printSubsequence(string input, string output) { if (input.empty()) { cout << output << endl; return; } printSubsequence(input.substr(1), output + input[0]); printSubsequence(input.substr(1), output); } int main() { string output = ""; string input = "rudraabonikoaa"; printSubsequence(input, output); return 0; }
Output
rudraabonikoaa rudraabonikoa rudraabonikoa rudraaboniko rudraabonikaa rudraabonika rudraabonika rudraabonik rudraabonioaa rudraabonioa rudraabonioa rudraabonio rudraaboniaa rudraabonia rudraabonia
Transform a string such that it has abcd…z as a subsequence
Here is the particular process to transform a string such that it has abcd...z as a subsequence.
Initialize the character.
If the length is less than 26, return false.
Iterate the loop to 0 to s.size () -1.
If character reaches to z, break the loop.
If the present character is less than s or equal to the character.
Replace the current character increment by 1.
If character less than or equal to z, then return false.
Else, return true.
Here for this process the time complexity is O(n) and the auxiliary space is O(1). Here, n is the length of a particular string.
Example 5
#include <bits/stdc++.h> using namespace std; bool transformString(string& s) { char ch = 'a'; if (s.size() < 26) return false; for (int i = 0; i < s.size(); i++) { if (int(ch) > int('z')) break; if (s[i] <= ch) { s[i] = ch; ch = char(int(ch) + 1); } } if (ch <= 'z') return false; return true; } int main() { string str = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"; if (transformString(str)) cout << str << endl; else cout << "Not Possible" << endl; return 0; }
Output
abcdefghijklmnopqrstuvwxyza
Conclusion
Today in this article we have learned about string transformation and its different forms by using C++ environment. By following the particular algorithm and syntax, we have examined and built some different C++ codes and, came to know how to transform a string such that it has abcd...z as a subsequence.