在C ++中打印以Vowel开头和以辅音结尾的所有字符串子序列
在这个问题中,我们给了一个字符串,我们必须从给定的字符串中找到子字符串。要找到的子字符串应以元音开头,并以恒定字符结尾。
甲串是字符数组。
可以通过删除字符串中的某些字符来生成此问题中要生成的子字符串。并且无需更改字符串的顺序。
Input: ‘abc’ Output: ab, ac, abc
为了解决这个问题,我们将迭代字符串并修复元音并检查下一个序列。让我们看看找到解决方案的算法-
算法
Step 1: Iterate of each character of the string, with variable i. Step 2: If the ith character is a vowel. Step 3: If the jth character is a consonant. Step 4: Add to the HashSet, substring from 1st character to jth character. Step 5: Repeat the following steps and find substrings from the string.
示例
#include <bits/stdc++.h>
using namespace std;
set<string> st;
bool isaVowel(char c);
bool isaConsonant(char c);
void findSubSequence(string str);
int main(){
string s = "abekns";
findSubSequence(s);
cout<<"The substring generated are :\n";
for (auto i : st)
cout<<i<<" ";
cout << endl;
return 0;
}
bool isaVowel(char c) {
return (c=='a'||c=='e'||c=='i'||c=='o'||c=='u');
}
bool isaConsonant(char c) {
return !isaVowel(c);
}
void findSubSequence(string str) {
for (int i = 0; i < str.length(); i++) {
if (isaVowel(str[i])) {
for (int j = str.length() - 1; j >= i; j--) {
if (isaConsonant(str[j])) {
string str_sub = str.substr(i, j + 1);
st.insert(str_sub);
for (int k = 1; k < str_sub.length() - 1; k++){
string sb = str_sub;
sb.erase(sb.begin() + k);
findSubSequence(sb);
}
}
}
}
}
}输出结果
生成的子字符串为-
ab abek abekn abekns abeks aben abens abes abk abkn abkns abks abn abns abs aek aekn aekns aeks aen aens aes ak akn akns aks an ans as ek ekn ekns eks en ens es
热门推荐
10 小学毕业父母简短祝福语
11 幼儿生日祝福语模板简短
12 新娘妈妈的祝福语简短
13 邻居聚会祝福语大全简短
14 十条祝福语简短
15 给同学祝福语的简短
16 过年祝福语简短祝妈妈
17 祝女儿简短祝福语大全
18 恩师诗词祝福语简短大全