Count the sum of count of distinct characters present in all Substrings
Last Updated :
15 Nov, 2023
Given a string S consisting of lowercase English letters of size N where (1 <= N <= 105), the task is to print the sum of the count of distinct characters N where (1 <= N <= 105)in all the substrings.
Examples:
Input: str = “abbca”
Output: 28
Explanation: The following are the substrings of “abbca”:
=> Sum of distinct characters in Substrings of length 1: “a”, “b”, “b”, “c”, “a” is 1 + 1 + 1 + 1 + 1 = 5
=> Sum of distinct characters in Substrings of length 2: “ab”, “bb”, “bc”, “ca” is 2 + 1 + 2 + 2 = 7.
=> Sum of distinct characters in Substrings of length 3: “abb”, “bbc”, “bca” is 2 + 2 + 3 = 7.
=> Sum of distinct characters in Substrings of length 4: “abbc”, “bbca” is 3 + 3 = 6.
=> Sum of distinct characters in Substrings of length 5: “abbca” is 3
The total count is 5 + 7 + 7 + 6 + 3 = 28
Input: str = “code”
Output: 20
Explanation: The following are the substrings of “code”:
=> Sum of distinct characters in Substrings of length 1: “c”, “o”, “d”, “e” is 1 + 1 + 1 + 1 = 4.
=> Sum of distinct characters in Substrings of length 2: “co”, “od”, “de” is 2 + 2 + 2 = 6.
=> Sum of distinct characters in Substrings of length 3: “cod”, “ode” is 3 + 3 = 6.
=> Sum of distinct characters in Substrings of length 4: “code” is 4.
The total count is 4+ 6 + 6 + 4 = 20
Approach: To solve the problem follow the below idea:
The idea is to count the contribution of each character in different substrings. For each character ch, iterate through the entire string and for each index i, we will count the number of substrings ending at i and have at least one occurrence of ch. This can be simply done by keeping track of the last occurrence of ch.
- Initialize a variable result with 0 to store the answer.
- Iterates through each lowercase English alphabet character from ‘a‘ to ‘z’.
- For each character, initializes a variable last_occur to -1, this will keep track of the position of the last occurrence of the current character in the string.
- Iterates through input string S and checks if the S[i] matches the current character being considered.
- If matches, it adds the 1-based index of the character to the result as these many substrings have at least one current alphabets and updates last_occur to the current position.
- If not matches, it adds the position of the last occurrence of the current character (stored in last_occur) to the result.
- Final return the result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long fun(string s)
{
int n = s.size();
long long result = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
int last_occur = -1;
for ( int i = 0; i < n; i++) {
if (s[i] == ch) {
result += (i + 1);
last_occur = i;
}
else {
result += (last_occur + 1);
}
}
}
return result;
}
int main()
{
string s = "abbca" ;
cout << fun(s);
return 0;
}
|
Java
public class GFG {
static long fun(String s)
{
int n = s.length();
long result = 0 ;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
int lastOccur = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == ch) {
result += (i + 1 );
lastOccur = i;
}
else {
result += (lastOccur + 1 );
}
}
}
return result;
}
public static void main(String[] args)
{
String s = "abbca" ;
System.out.println(fun(s));
}
}
|
Python3
def fun(s):
n = len (s)
result = 0
for ch in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
ch = chr (ch)
last_occur = - 1
for i in range (n):
if s[i] = = ch:
result + = (i + 1 )
last_occur = i
else :
result + = (last_occur + 1 )
return result
s = "abbca"
print (fun(s))
|
C#
using System;
class Program
{
static long Fun( string s)
{
int n = s.Length;
long result = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
int last_occur = -1;
for ( int i = 0; i < n; i++)
{
if (s[i] == ch)
{
result += (i + 1);
last_occur = i;
}
else
{
result += (last_occur + 1);
}
}
}
return result;
}
static void Main()
{
string s = "abbca" ;
Console.WriteLine(Fun(s));
}
}
|
Javascript
function fun(s) {
const n = s.length;
let result = 0;
for (let ch = 'a' ; ch <= 'z' ; ch = String.fromCharCode(ch.charCodeAt(0) + 1)) {
let lastOccur = -1;
for (let i = 0; i < n; i++) {
if (s[i] === ch) {
result += (i + 1);
lastOccur = i;
} else {
result += (lastOccur + 1);
}
}
}
return result;
}
function main() {
const s = "abbca" ;
console.log(fun(s));
}
main();
|
Time Complexity: O(26*N), where N is the length of the input string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...