📌  相关文章
📜  一个字符串在另一个给定字符串的最大连续出现次数

📅  最后修改于: 2021-09-07 05:08:38             🧑  作者: Mango

鉴于两个字符串str1str2的,任务是计数字符串STR2的最大连续出现在字符串STR1。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量,比如cntOcc ,以存储字符串str1str2的出现次数。
  • 迭代范围[CntOcc, 1] 。对于迭代中的每个i值,将字符串str2连接i次并检查连接的字符串是否是字符串str1的子字符串。发现它为真的第i值,将其打印为所需答案。

下面是实现:

C++
// C++ program to implement
// the above approach
#include 
#include 
using namespace std;
 
int countFreq(string& pat, string& txt)
{
    int M = pat.length();
    int N = txt.length();
    int res = 0;
 
    // A loop to slide pat[] one by one
    for(int i = 0; i <= N - M; i++)
    {
         
        // For current index i, check
        // for pattern match
        int j;
        for(j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
 
        // If pat[0...M-1] = txt[i, i+1, ...i+M-1]
        if (j == M)
        {
            res++;
            j = 0;
        }
    }
    return res;
}
 
// Function to count the maximum
// consecutive occurrence of the
// string str2 in in the string str1
int maxRepeating(string str1, string str2)
{
     
    // Stores the count of consecutive
    // occurrences of str2 in str1
    int cntOcc = countFreq(str2, str1);
     
    // Concatenate str2 cntOcc times
    string Contstr = "";
 
    for(int i = 0; i < cntOcc; i++)
        Contstr += str2;
 
    // Iterate over the string str1
    // while Contstr is not present in str1
    size_t found = str1.find(Contstr);
     
    while (found == string::npos)
    {
        found = str1.find(Contstr);
         
        // Update cntOcc
        cntOcc -= 1;
 
        // Update Contstr
        Contstr = "";
         
        for(int i = 0; i < cntOcc; i++)
            Contstr += str2;
    }
    return cntOcc;
}
 
// Driver Code
int main()
{
    string str1 = "abababc";
    string str2 = "ba";
     
    cout << maxRepeating(str1, str2);
 
    return 0;
}
 
// This code is contributed by grand_master


Java
// Java program to implement
// the above approach
 
import java.util.*;
class GFG
{
static int countFreq(String pat, String txt)
{
    int M = pat.length();
    int N = txt.length();
    int res = 0;
 
    // A loop to slide pat[] one by one
    for(int i = 0; i <= N - M; i++)
    {
         
        // For current index i, check
        // for pattern match
        int j;
        for(j = 0; j < M; j++)
            if (txt.charAt(i + j) != pat.charAt(j))
                break;
 
        // If pat[0...M-1] = txt[i, i+1, ...i+M-1]
        if (j == M)
        {
            res++;
            j = 0;
        }
    }
    return res;
}
 
// Function to count the maximum
// consecutive occurrence of the
// String str2 in in the String str1
static int maxRepeating(String str1, String str2)
{
     
    // Stores the count of consecutive
    // occurrences of str2 in str1
    int cntOcc = countFreq(str2, str1);
     
    // Concatenate str2 cntOcc times
    String Contstr = "";
 
    for(int i = 0; i < cntOcc; i++)
        Contstr += str2;
 
    // Iterate over the String str1
    // while Contstr is not present in str1
    boolean found = str1.contains(Contstr);
     
    while (!found)
    {
        found = str1.contains(Contstr);
         
        // Update cntOcc
        cntOcc -= 1;
 
        // Update Contstr
        Contstr = "";
         
        for(int i = 0; i < cntOcc; i++)
            Contstr += str2;
    }
    return cntOcc;
}
 
// Driver Code
public static void main(String[] args)
{
    String str1 = "abababc";
    String str2 = "ba"; 
    System.out.print(maxRepeating(str1, str2));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to count the maximum
# consecutive occurrence of the
# string str2 in in the string str1
def maxRepeating(str1, str2):
 
    # Stores the count of consecutive
    # occurrences of str2 in str1
    cntOcc = str1.count(str2)
 
    # Concatenate str2 cntOcc times
    Contstr = str2 * cntOcc
 
     
    # Iterate over the string str1
    # while Contstr is not present in str1
    while(Contstr not in str1):
 
        # Update cntOcc
        cntOcc -= 1
         
       # Update Contstr
        Contstr = str2 * cntOcc
         
    return cntOcc
 
# Driver Code
if __name__ =="__main__":
  str1 = "abababc"
  str2 = "ba"
  print(maxRepeating(str1, str2))


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
static int countFreq(String pat, String txt)
{
    int M = pat.Length;
    int N = txt.Length;
    int res = 0;
 
    // A loop to slide pat[] one by one
    for(int i = 0; i <= N - M; i++)
    {
         
        // For current index i, check
        // for pattern match
        int j;
        for(j = 0; j < M; j++)
            if (txt[i + j] != pat[j])
                break;
 
        // If pat[0...M-1] = txt[i, i+1, ...i+M-1]
        if (j == M)
        {
            res++;
            j = 0;
        }
    }
    return res;
}
 
// Function to count the maximum
// consecutive occurrence of the
// String str2 in in the String str1
static int maxRepeating(String str1, String str2)
{
     
    // Stores the count of consecutive
    // occurrences of str2 in str1
    int cntOcc = countFreq(str2, str1);
     
    // Concatenate str2 cntOcc times
    String Contstr = "";
 
    for(int i = 0; i < cntOcc; i++)
        Contstr += str2;
 
    // Iterate over the String str1
    // while Contstr is not present in str1
    bool found = str1.Contains(Contstr);   
    while (!found)
    {
        found = str1.Contains(Contstr);
         
        // Update cntOcc
        cntOcc -= 1;
 
        // Update Contstr
        Contstr = "";
         
        for(int i = 0; i < cntOcc; i++)
            Contstr += str2;
    }
    return cntOcc;
}
 
// Driver Code
public static void Main(String[] args)
{
    String str1 = "abababc";
    String str2 = "ba"; 
    Console.Write(maxRepeating(str1, str2));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度: O(N 2 )
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live