Given a string S consisting of lowercase alphabets of size N, the task is to count all substrings which contain the most frequent character in the string as the first character.
Note: If more than one character has a maximum frequency, consider the lexicographically smallest among them.
Examples:
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
Approach: The idea is to first find the character that occurs the maximum number of times and then count the substring starting with that character in the string. Follow the steps below to solve the problem:
- Initialize the count as 0 that will store the total count of strings.
- Find the maximum occurring character in the string S. Let that character be ch.
- Traverse the string using the variable i and if the character at ith index is the same as ch, increment the count by (N – i).
- After the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int substringCount(string s)
{
vector< int > freq(26, 0);
char max_char = '#' ;
int maxfreq = INT_MIN;
for ( int i = 0; i < s.size(); i++)
{
freq[s[i] - 'a' ]++;
if (maxfreq < freq[s[i] - 'a' ])
maxfreq = freq[s[i] - 'a' ];
}
for ( int i = 0; i < 26; i++)
{
if (maxfreq == freq[i])
{
max_char = ( char )(i + 'a' );
break ;
}
}
int ans = 0;
for ( int i = 0; i < s.size(); i++)
{
char ch = s[i];
if (max_char == ch)
{
ans += (s.size() - i);
}
}
return ans;
}
int main()
{
string S = "abcab" ;
cout << (substringCount(S));
}
|
Java
import java.util.*;
class GFG {
static int substringCount(String s)
{
int [] freq = new int [ 26 ];
char max_char = '#' ;
int maxfreq = Integer.MIN_VALUE;
for ( int i = 0 ;
i < s.length(); i++) {
freq[s.charAt(i) - 'a' ]++;
if (maxfreq
< freq[s.charAt(i) - 'a' ])
maxfreq
= freq[s.charAt(i) - 'a' ];
}
for ( int i = 0 ; i < 26 ; i++) {
if (maxfreq == freq[i]) {
max_char = ( char )(i + 'a' );
break ;
}
}
int ans = 0 ;
for ( int i = 0 ;
i < s.length(); i++) {
char ch = s.charAt(i);
if (max_char == ch) {
ans += (s.length() - i);
}
}
return ans;
}
public static void main(String[] args)
{
String S = "abcab" ;
System.out.println(substringCount(S));
}
}
|
Python3
import sys
def substringCount(s):
freq = [ 0 for i in range ( 26 )]
max_char = '#'
maxfreq = - sys.maxsize - 1
for i in range ( len (s)):
freq[ ord (s[i]) - ord ( 'a' )] + = 1
if (maxfreq < freq[ ord (s[i]) - ord ( 'a' )]):
maxfreq = freq[ ord (s[i]) - ord ( 'a' )]
for i in range ( 26 ):
if (maxfreq = = freq[i]):
max_char = chr (i + ord ( 'a' ))
break
ans = 0
for i in range ( len (s)):
ch = s[i]
if (max_char = = ch):
ans + = ( len (s) - i)
return ans
if __name__ = = '__main__' :
S = "abcab"
print (substringCount(S))
|
C#
using System;
class GFG{
static int substringCount( string s)
{
int [] freq = new int [26];
char max_char = '#' ;
int maxfreq = Int32.MinValue;
for ( int i = 0; i < s.Length; i++)
{
freq[s[i] - 'a' ]++;
if (maxfreq < freq[s[i] - 'a' ])
maxfreq = freq[s[i] - 'a' ];
}
for ( int i = 0; i < 26; i++)
{
if (maxfreq == freq[i])
{
max_char = ( char )(i + 'a' );
break ;
}
}
int ans = 0;
for ( int i = 0; i < s.Length; i++)
{
char ch = s[i];
if (max_char == ch)
{
ans += (s.Length - i);
}
}
return ans;
}
public static void Main()
{
string S = "abcab" ;
Console.WriteLine(substringCount(S));
}
}
|
Javascript
<script>
function substringCount(s)
{
var freq = new Array(26).fill(0);
var max_char = "#" ;
var maxfreq = -21474836487;
for ( var i = 0; i < s.length; i++)
{
freq[s[i].charCodeAt(0) -
"a" .charCodeAt(0)]++;
if (maxfreq < freq[s[i].charCodeAt(0) -
"a" .charCodeAt(0)])
maxfreq = freq[s[i].charCodeAt(0) -
"a" .charCodeAt(0)];
}
for ( var i = 0; i < 26; i++)
{
if (maxfreq === freq[i])
{
max_char = String.fromCharCode(
i + "a" .charCodeAt(0));
break ;
}
}
var ans = 0;
for ( var i = 0; i < s.length; i++)
{
var ch = s[i];
if (max_char === ch)
{
ans += s.length - i;
}
}
return ans;
}
var S = "abcab" ;
document.write(substringCount(S));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse the string.
Auxiliary Space: O(1), as we are using freq array of size 26 which is constant.