
在这个问题中,我们需要分割给定的字符串,使得第三个子字符串可以是前两个子字符串的子字符串。
让我们想想解决办法。仅当前两个字符串包含第三个字符串的所有字符时,第三个字符串才可以是前两个字符串的子字符串。所以,我们需要在给定的字符串中找到至少一个出现频率大于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’。
在这种方法中,我们将使用一个数组来存储每个字符的频率。之后,我们将检查频率大于或等于3的字符。
步骤 1 - 定义长度等于 26 的“freq”数组。
步骤 2 - 遍历字符串以计算字符的频率。在 for 循环中,增加 freq[str[i] – ‘a’] 的值。这里,str[i] – ‘a’给出0到26之间的索引。
第 3 步 - 现在,遍历“freq”数组,如果任意数组索引处的值大于“3”,则返回 true。
第 4 步 - 循环终止时返回 true。
第 5 步 - 根据 isSUbStringPossible() 函数的返回值打印“是”或“否”。
#include <bits/stdc++.h>
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 two
string 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 < 26; i++){
// If the frequency of any character is greater than or equal to 3, then return "Yes."
if (freq[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),因为我们使用恒定长度的数组。
在这种方法中,我们首先将字符串转换为字符数组。之后,我们使用 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 <bits/stdc++.h>
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 two
string 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 < 26; i++){
// Using count() to count the occurrence of each character in the array, and return 'yes' if any character occurs more than 2 times.
if (count(char_array, char_array + len, i + 'a') >= 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),因为我们将字符串存储到字符数组中。
我们学习了两种将字符串拆分为三个子字符串的方法,这样一个子字符串可以成为另外两个子字符串的子字符串。第二种方法的代码更具可读性,对初学者更友好,但时间和空间成本更高。
以上就是检查一个字符串是否可以被分割成三个子字符串,其中一个子字符串是另外两个子字符串的子串的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号