给定字符串S由大小为N的小写字母组成,任务是将包含该字符串中最频繁出现的字符的所有子字符串为第一个字符。
注意:如果一个以上的字符具有最高的频率,则在字典上考虑最小的字符。
例子:
Input: S = “abcab”
Output: 7
Explanation:
There are two characters a and b occurring maximum times i.e., 2 times.
Selecting the lexicographically smaller character i.e. ‘a’.
Substrings starts with ‘a’ are: “a”, “ab”, “abc”, “abca”, “abcab”, “a”, “ab”.
Therefore the count is 7.
Input: S= “cccc”
Output: 10
方法:我们的想法是先找到出现的最大次数,然后算子串开头的字符串中的字符的字符。请按照以下步骤解决问题:
- 将计数初始化为0 ,该计数将存储字符串的总数。
- 在字符串S中找到最大出现的字符。让该字符为ch 。
- 使用变量i遍历字符串,如果第i个索引处的字符与ch相同,则将计数增加(N – i) 。
- 完成上述步骤后,将count的值打印为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all substrings
// whose first character occurs
// maximum number of times
int substringCount(string s)
{
// Stores freqency of characters
vector freq(26, 0);
// Stores character that appears
// maximum number of times
char max_char = '#';
// Stores max freqency of character
int maxfreq = INT_MIN;
// Updates frequecy of characters
for(int i = 0; i < s.size(); i++)
{
freq[s[i] - 'a']++;
// Update maxfreq
if (maxfreq < freq[s[i] - 'a'])
maxfreq = freq[s[i] - 'a'];
}
// Character that occures
// maximum number of times
for(int i = 0; i < 26; i++)
{
// Update the maximum frequency
// character
if (maxfreq == freq[i])
{
max_char = (char)(i + 'a');
break;
}
}
// Stores all count of substrings
int ans = 0;
// Traverse over string
for(int i = 0; i < s.size(); i++)
{
// Get the current character
char ch = s[i];
// Update count of substrings
if (max_char == ch)
{
ans += (s.size() - i);
}
}
// Return the count of all
// valid substrings
return ans;
}
// Driver Code
int main()
{
string S = "abcab";
// Function Call
cout << (substringCount(S));
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find all substrings
// whose first character occurs
// maximum number of times
static int substringCount(String s)
{
// Stores freqency of characters
int[] freq = new int[26];
// Stores character that appears
// maximum number of times
char max_char = '#';
// Stores max freqency of character
int maxfreq = Integer.MIN_VALUE;
// Updates frequecy of characters
for (int i = 0;
i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
// Update maxfreq
if (maxfreq
< freq[s.charAt(i) - 'a'])
maxfreq
= freq[s.charAt(i) - 'a'];
}
// Character that occures
// maximum number of times
for (int i = 0; i < 26; i++) {
// Update the maximum frequency
// character
if (maxfreq == freq[i]) {
max_char = (char)(i + 'a');
break;
}
}
// Stores all count of substrings
int ans = 0;
// Traverse over string
for (int i = 0;
i < s.length(); i++) {
// Get the current character
char ch = s.charAt(i);
// Update count of substrings
if (max_char == ch) {
ans += (s.length() - i);
}
}
// Return the count of all
// valid substrings
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "abcab";
// Function Call
System.out.println(substringCount(S));
}
}
Python3
# Python3 program for the above approach
import sys
# Function to find all substrings
# whose first character occurs
# maximum number of times
def substringCount(s):
# Stores freqency of characters
freq = [0 for i in range(26)]
# Stores character that appears
# maximum number of times
max_char = '#'
# Stores max freqency of character
maxfreq = -sys.maxsize - 1
# Updates frequecy of characters
for i in range(len(s)):
freq[ord(s[i]) - ord('a')] += 1
# Update maxfreq
if (maxfreq < freq[ord(s[i]) - ord('a')]):
maxfreq = freq[ord(s[i]) - ord('a')]
# Character that occures
# maximum number of times
for i in range(26):
# Update the maximum frequency
# character
if (maxfreq == freq[i]):
max_char = chr(i + ord('a'))
break
# Stores all count of substrings
ans = 0
# Traverse over string
for i in range(len(s)):
# Get the current character
ch = s[i]
# Update count of substrings
if (max_char == ch):
ans += (len(s) - i)
# Return the count of all
# valid substrings
return ans
# Driver Code
if __name__ == '__main__':
S = "abcab"
# Function Call
print(substringCount(S))
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to find all substrings
// whose first character occurs
// maximum number of times
static int substringCount(string s)
{
// Stores freqency of characters
int[] freq = new int[26];
// Stores character that appears
// maximum number of times
char max_char = '#';
// Stores max freqency of character
int maxfreq = Int32.MinValue;
// Updates frequecy of characters
for(int i = 0; i < s.Length; i++)
{
freq[s[i] - 'a']++;
// Update maxfreq
if (maxfreq < freq[s[i] - 'a'])
maxfreq = freq[s[i] - 'a'];
}
// Character that occures
// maximum number of times
for(int i = 0; i < 26; i++)
{
// Update the maximum frequency
// character
if (maxfreq == freq[i])
{
max_char = (char)(i + 'a');
break;
}
}
// Stores all count of substrings
int ans = 0;
// Traverse over string
for(int i = 0; i < s.Length; i++)
{
// Get the current character
char ch = s[i];
// Update count of substrings
if (max_char == ch)
{
ans += (s.Length - i);
}
}
// Return the count of all
// valid substrings
return ans;
}
// Driver Code
public static void Main()
{
string S = "abcab";
// Function Call
Console.WriteLine(substringCount(S));
}
}
// This code is contributed by susmitakundugoaldanga
输出:
7
时间复杂度: O(N)
辅助空间: O(1)