给定一个字符串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 frequency 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 frequency 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 frequency 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 frequency 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
Javascript
输出:
7
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。