公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

在这个问题中,我们需要分割给定的字符串,使得第三个子字符串可以是前两个子字符串的子字符串。

让我们想想解决办法。仅当前两个字符串包含第三个字符串的所有字符时,第三个字符串才可以是前两个字符串的子字符串。所以,我们需要在给定的字符串中找到至少一个出现频率大于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 <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),因为我们使用恒定长度的数组。

    方法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 <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),因为我们将字符串存储到字符数组中。

      结论

      我们学习了两种将字符串拆分为三个子字符串的方法,这样一个子字符串可以成为另外两个子字符串的子字符串。第二种方法的代码更具可读性,对初学者更友好,但时间和空间成本更高。

      以上就是检查一个字符串是否可以被分割成三个子字符串,其中一个子字符串是另外两个子字符串的子串的详细内容,更多请关注www.xfxf.net其它相关文章!

分享到:
标签:子字符串 字符串分割 检查
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定