
在这个问题中,我们需要分割给定的字符串,使得第三个子字符串可以是前两个子字符串的子字符串。
让我们想想解决办法。仅当前两个字符串包含第三个字符串的所有字符时,第三个字符串才可以是前两个字符串的子字符串。所以,我们需要在给定的字符串中找到至少一个出现频率大于3的字符,并且我们可以取该单个字符的第三个子串。
问题陈述 – 我们给出了一个包含 N 个小写字母字符的字符串 str。我们需要检查是否可以将字符串拆分为三个子字符串 a、b 和 c,使得子字符串 c 是 a 和 b 的子字符串。根据是否能找到 3 个子串,打印“yes”或“no”。
示例
Input – str = "tutorialsPoint"
Output – ‘Yes’
说明
在这里,我们可以将字符串拆分为“tu”、“torialsPoin”和“t”。因此,第三个字符串是前两个字符串的子字符串。
Input – str = "tutorials"
Output – ‘No’
说明
我们无法根据给定条件将字符串拆分为三个子字符串,因为任何字符的频率都不大于 3。
Input – str = "hhhhhhhh"
Output – ‘Yes’
说明
根据给定条件,三个子字符串可以是‘h’、‘h’和‘hhhhhh’。
方法 1
在这种方法中,我们将使用一个数组来存储每个字符的频率。之后,我们将检查频率大于或等于3的字符。
算法
步骤 1 – 定义长度等于 26 的“freq”数组。
步骤 2 – 遍历字符串以计算字符的频率。在 for 循环中,增加 freq[str[i] – ‘a’] 的值。这里,str[i] – ‘a’给出0到26之间的索引。
第 3 步 – 现在,遍历“freq”数组,如果任意数组索引处的值大于“3”,则返回 true。
第 4 步 – 循环终止时返回 true。
第 5 步 – 根据 isSUbStringPossible() 函数的返回值打印“是”或“否”。
示例
#include using namespace std;// function to Check if a string can be split into 3 substrings such that one of them is a substring of the other twostring isSubStringPossible(string str, int len){ // array to store the frequency int freq[26] = {0}; // Iterate over the string for (int i = 0; i < len; i++){ // count the frequency of each character freq[str[i] - 'a']++; } // Traverse the frequency array for (int i = 0; i = 3){ return "Yes"; } } // Otherwise return "No";}int main(){ string str = "tutorialsPoint"; int len = str.length(); cout << "The given string can be splited into 3 substrings such that one of them is a substring of the other two - " << isSubStringPossible(str, len); return 0;}
输出
The given string can be splited into 3 substrings such that one of them is a substring of the other two - Yes
时间复杂度 – O(N),当我们遍历字符串时。
空间复杂度 – O(1),因为我们使用恒定长度的数组。
方法2
在这种方法中,我们首先将字符串转换为字符数组。之后,我们使用 count() 方法来统计数组中特定字符的出现频率。
算法
第 1 步 – 创建一个大小为“len + 1”的数组,其中“len”是字符串长度。
第 2 步 – 使用 strcpy() 方法将字符串复制到 char 数组中。
第 3 步 – 使用 for 循环进行总共 26 次迭代。
步骤 4 – 在 for 循环中,使用 count() 方法来计算特定字符的出现次数。
count() 方法将对开始位置的引用作为第一个参数,对结束位置的引用作为第二个参数,以及一个字符作为第三个参数。
在这里,我们需要将字符的 ASCII 值作为参数传递,我们使用 I + ‘a’ 来获取该值。
第 5 步 – 如果 count() 方法返回大于或等于 3,则返回 true。
第 6 步 – 循环终止时返回 false。
示例
#include using namespace std;// function to Check if a string can be split into 3 substrings such that one of them is a substring of the other twostring isSubStringPossible(string str, int len){ // converting str to char array. char char_array[len + 1]; // copy string to char array strcpy(char_array, str.c_str()); // make 26 iterations for (int i = 0; i = 2) return "YES"; } return "NO";}int main(){ string str = "tutorials"; int len = str.length(); cout << "The given string can be splited into 3 substrings such that one of them is a substring of the other two - " << isSubStringPossible(str, len); return 0;}
输出
The given string can be splited into 3 substrings such that one of them is a substring of the other two - Yes
时间复杂度 – O(N),因为 count() 方法迭代 char 数组来计算字符数。另外,strcpy() 方法需要 O(N) 时间。
空间复杂度 – O(N),因为我们将字符串存储到字符数组中。
结论
我们学习了两种将字符串拆分为三个子字符串的方法,这样一个子字符串可以成为另外两个子字符串的子字符串。第二种方法的代码更具可读性,对初学者更友好,但时间和空间成本更高。
以上就是检查一个字符串是否可以被分割成三个子字符串,其中一个子字符串是另外两个子字符串的子串的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445604.html
微信扫一扫
支付宝扫一扫