Home > Backend Development > C++ > Convert a string to a form that has abcd..z as a subsequence

Convert a string to a form that has abcd..z as a subsequence

WBOY
Release: 2023-09-01 14:17:06
forward
1176 people have browsed it

Convert a string to a form that has abcd..z as a subsequence

String conversion (also known as string conversion) is an operation in C that stores the result in an output array after the entire process is executed. In C, there is a function called "transform()", which exists in the directory of the C environment, through which we can convert a string into a new string.

There are two forms of conversion functions −

  • Unary operation

    • The operation is applied to each element of the input array.

    • After the operation is completed, the results will be stored in an output array.

  • Binary operations

  • The operation applies to each element of a specific array.

  • The first input element and the second corresponding input element participate in the operation.

  • The output data will be stored in an output array.

A subsequence string is a brand new string generated by performing various operations on the input string (for example: deletion). For subsequence strings, the operation occurs without affecting the remaining characters.

For string conversion, the input contains an operation string of length n 1. The original characters belong to the series a to z. The length of the print string is treated as n here, which is an output string.

In this article, we will learn how to convert a string in C environment to have abcd….z as a subsequence.

Recursive algorithm generates subsequent strings

By using a recursive approach, the following is a possible algorithm for a subsequent string. This is a specific string and T is the time it takes to complete the operation.

  • Step 1 - Count the number of occurrences.

  • Step 2 - If i = length(s) and j = length(T).

  • The third step − then returns 1.

  • Step 4 - End.

  • Step 5 - If i = length(S).

  • Step 6 - Then return 0.

  • Step 7 - End.

  • Step 8 − Count

  • Step 9 - If, j

  • Step 10 − Count

  • Step 11 - End.

  • Step 12 - Count

  • Step 13 - Return the count.

  • Step 14 - End.

Syntax for subsequent arrays

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])
Copy after login

Here we create a basic working syntax for subsequent arrays. When there are two sequences, we have to follow the following steps to get the output.

Following method

  • Method 1−Use C to convert string

  • Method 2 for unary operations on strings using C

  • Method 3 of using C to perform binary operations on strings

  • Use C to print all possible subsequent strings

  • Method to convert string to having abcd….z as subsequence using C 5

Convert string using C

In this C code, we create a new string and remove all vowels from the input string. # is added in place of these 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;
}
Copy after login

Output

#r#D#R#d#V#d#S!!
-1
Copy after login

Use C to perform unary operations on strings

In this particular code, we show how to perform unary operations on the input array. This function accepts a pointer to the start and end position of a single input. And operate at the beginning of the output array.

The Chinese translation of

Example 2

is:

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;
}
Copy after login

Output

Input array present here: 7 16 10 97 2001
The output array now contains with: 8 17 11 98 2002
Copy after login

Use C to perform binary operations on strings

In this particular code, we show how to perform binary operations on the input array. The function transform() adds a pointer between the starting point and the first input array. Remember that binary operations always operate on two input data sets.

The Chinese translation of

Example 3

is:

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;
}
Copy after login

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
Copy after login

Use C to print all subsequent strings

Apply the concepts of selection and non-selection to find all subsequences of a specific array. During this process, some characters may be removed without changing the order of the elements. Here, the time complexity of this process is O(2^n) and the 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;
}
Copy after login

Output

rudraabonikoaa
rudraabonikoa
rudraabonikoa
rudraaboniko
rudraabonikaa
rudraabonika
rudraabonika
rudraabonik
rudraabonioaa
rudraabonioa
rudraabonioa
rudraabonio
rudraaboniaa
rudraabonia
rudraabonia
Copy after login

Convert a string to have abcd...z as a subsequence

This is a specific procedure for converting a string into a form that has abcd...z as a subsequence.

  • Initialization characters.

  • If the length is less than 26, return false.

  • Iterate the loop from 0 to s.size() - 1.

  • If the character reaches z, break out of the loop.

  • If the current character is less than s or equal to character.

  • Replace the increment of the current character with 1.

  • If the character is less than or equal to z, return false.

  • Otherwise, return true.

在这个过程中,时间复杂度为O(n),辅助空间为O(1)。这里,n是特定字符串的长度。

Example 5

的中文翻译为:

示例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;
}
Copy after login

输出

abcdefghijklmnopqrstuvwxyza
Copy after login

结论

在本文中,我们学习了使用C++环境进行字符串转换及其不同形式。通过遵循特定的算法和语法,我们检查和构建了一些不同的C++代码,并了解了如何转换字符串,使其具有abcd...z作为子序列。

The above is the detailed content of Convert a string to a form that has abcd..z as a subsequence. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template