给定一个字符串str和一个整数k 。任务是计算由相同字符组成的长度为k的子串的出现次数。可以有多个这样的长度为 k 的子串,选择出现次数最多的那个作为str的子串(不重叠)。
例子:
Input: str = “aaacaabbaa”, k = 2
Output: 3
“aa” and “bb” are the only sub-strings of length 2 that consist of the same characters.
“bb” appears only once as a sub-string of str whereas “aa” appears thrice (which is the answer)
Input: str = “abab”, k = 2
Output: 0
方法:迭代所有从字符“a”到“Z”和计数长度为k的仅由当前字符的字符串显示为STR的子串的次数。最后打印这些计数的最大值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of the required sub-strings
int maxSubStrings(string s, int k)
{
int maxSubStr = 0, n = s.size();
// Iterate over all characters
for (int c = 0; c < 26; c++) {
char ch = 'a' + c;
// Count with current character
int curr = 0;
for (int i = 0; i <= n - k; i++) {
if (s[i] != ch)
continue;
int cnt = 0;
while (i < n && s[i] == ch && cnt != k) {
i++;
cnt++;
}
i--;
// If the substring has a length k
// then increment count with current character
if (cnt == k)
curr++;
}
// Update max count
maxSubStr = max(maxSubStr, curr);
}
return maxSubStr;
}
// Driver Code
int main()
{
string s = "aaacaabbaa";
int k = 2;
cout << maxSubStrings(s, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Function to return the count
// of the required sub-strings
static int maxSubStrings(String s, int k)
{
int maxSubStr = 0, n = s.length();
// Iterate over all characters
for (int c = 0; c < 26; c++)
{
char ch = (char)((int)'a' + c);
// Count with current character
int curr = 0;
for (int i = 0; i <= n - k; i++)
{
if (s.charAt(i) != ch)
continue;
int cnt = 0;
while (i < n && s.charAt(i) == ch &&
cnt != k)
{
i++;
cnt++;
}
i--;
// If the substring has a length
// k then increment count with
// current character
if (cnt == k)
curr++;
}
// Update max count
maxSubStr = Math.max(maxSubStr, curr);
}
return maxSubStr;
}
// Driver Code
public static void main(String []args)
{
String s = "aaacaabbaa";
int k = 2;
System.out.println(maxSubStrings(s, k));
}
}
// This code is contributed by
// tufan_gupta2000
Python3
# Python 3 implementation of the approach
# Function to return the count
# of the required sub-strings
def maxSubStrings(s, k):
maxSubStr = 0
n = len(s)
# Iterate over all characters
for c in range(27):
ch = chr(ord('a') + c)
# Count with current character
curr = 0
for i in range(n - k):
if (s[i] != ch):
continue
cnt = 0
while (i < n and s[i] == ch and
cnt != k):
i += 1
cnt += 1
i -= 1
# If the substring has a length k then
# increment count with current character
if (cnt == k):
curr += 1
# Update max count
maxSubStr = max(maxSubStr, curr)
return maxSubStr
# Driver Code
if __name__ == '__main__':
s = "aaacaabbaa"
k = 2
print(maxSubStrings(s, k))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count
// of the required sub-strings
static int maxSubStrings(String s, int k)
{
int maxSubStr = 0, n = s.Length;
// Iterate over all characters
for (int c = 0; c < 26; c++)
{
char ch = (char)((int)'a' + c);
// Count with current character
int curr = 0;
for (int i = 0; i <= n - k; i++)
{
if (s[i] != ch)
continue;
int cnt = 0;
while (i < n && s[i] == ch &&
cnt != k)
{
i++;
cnt++;
}
i--;
// If the substring has a length
// k then increment count with
// current character
if (cnt == k)
curr++;
}
// Update max count
maxSubStr = Math.Max(maxSubStr, curr);
}
return maxSubStr;
}
// Driver Code
public static void Main()
{
string s = "aaacaabbaa";
int k = 2;
Console.WriteLine(maxSubStrings(s, k));
}
}
// This code is contributed by Ryuga
Javascript
输出:
3
时间复杂度: O(n),其中 n 是字符串的长度。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。