Count Substrings with Frequencies Less than Maximum Digit
Last Updated :
31 Dec, 2023
Given a string S of length N. Then your task is to find the count of substrings such that, the frequency of each digit in that substring must be less than the maximum digit of substring.
Examples:
Input: S = 122321
Output: 13
Explanation: Below are some of those substrings:
- S[1, 2]: 12. Max element = 2, Frequency of 1 and 2 both is 1, Which is less than 2.
- S[1, 4]: 1223. Max element = 3, Frequencies of 1 and 2 and 3 are 1, 2 and 1 respectively. Which is less than 3.
- S[2]: 2. Max element = 2, Frequency of 2 is 1, Which is less than 2.
- S[2, 4]: 223. Max element = 3, Frequency of 2 and 3 both is 2 and 1, Which is less than 3.
- In the same way = {2, 23, 232, 2321, 3, 32, 321, 2, 21} are also other substrings satisfying the given conditions. So, there are 13 such substrings.
Input: S = 1231
Output: 8
Explanation: The 8 substrings, which satisfies the given condition are: {12, 123, 1231, 2, 23, 231, 3, 31}.
Approach:
As, the number of digits can only be 10. Starting from each index iterate for 72 times because substring of length greater than 72 can’t satisfy the given condition, because maximum element can be 9 and each elements frequency can not exceed 8 in this case so in worst case it will be 8*9 length long valid substring ). Check how many subarrays are possible, where in each subarray frequency of each character in the substring is strictly less than the maximum digit of the substring.
Steps were taken to solve the problem:
- Create a variable let say Ans to store the count of substrings.
- Run a loop for i = 0 to i < N and follow below mentioned conditions under the scope of loop:
- Create a vector let say Freq of length 10.
- Create two variables let say MaxNumber and MaxFrequency and initialize both to 0.
- Run a loop for j = i to j < Min(i+72, N) and follow below mentioned steps under the scope of loop:
- Increment the frequency of Jth digit of S in vector.
- Update MaxNumber with max(maxNumber, S[j] – ‘0’)
- Update MaxFrequency with max(maxFrequency, freq[S[j] – ‘0’])
- if (MaxNumber > MaxFrequency), then increment Ans.
- Return Ans.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int CountStrings(string S)
{
int N = S.size();
int ans = 0;
for ( int i = 0; i < N; i++) {
vector< int > freq(10, 0);
int maxNumber = 0;
int maxFrequency = 0;
for ( int j = i; j < min(i + 72, N); j++) {
freq[S[j] - '0' ]++;
maxNumber = max(maxNumber, S[j] - '0' );
maxFrequency
= max(maxFrequency, freq[S[j] - '0' ]);
if (maxNumber > maxFrequency)
ans++;
}
}
return ans;
}
int main()
{
string S = "123221";
cout << CountStrings(S);
return 0;
}
|
Java
import java.util.*;
public class Program {
static int countStrings(String S) {
int N = S.length();
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
HashMap<Character, Integer> freq = new HashMap<>();
int maxNumber = 0 ;
int maxFrequency = 0 ;
for ( int j = i; j < Math.min(i + 72 , N); j++) {
freq.put(S.charAt(j), freq.getOrDefault(S.charAt(j), 0 ) + 1 );
maxNumber = Math.max(maxNumber, S.charAt(j) - '0' );
maxFrequency = Math.max(maxFrequency, freq.get(S.charAt(j)));
if (maxNumber > maxFrequency) {
ans++;
}
}
}
return ans;
}
public static void main(String[] args) {
String S = "123221" ;
System.out.println(countStrings(S));
}
}
|
Python3
def count_strings(S):
N = len (S)
ans = 0
for i in range (N):
freq = [ 0 ] * 10
max_number = 0
max_frequency = 0
for j in range (i, min (i + 72 , N)):
freq[ int (S[j])] + = 1
max_number = max (max_number, int (S[j]))
max_frequency = max (max_frequency, freq[ int (S[j])])
if max_number > max_frequency:
ans + = 1
return ans
if __name__ = = "__main__" :
S = "123221"
print (count_strings(S))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountStrings( string S)
{
int N = S.Length;
int ans = 0;
for ( int i = 0; i < N; i++)
{
Dictionary< char , int > freq = new Dictionary< char , int >();
int maxNumber = 0;
int maxFrequency = 0;
for ( int j = i; j < Math.Min(i + 72, N); j++)
{
if (!freq.ContainsKey(S[j]))
freq[S[j]] = 0;
freq[S[j]]++;
maxNumber = Math.Max(maxNumber, S[j] - '0' );
maxFrequency = Math.Max(maxFrequency, freq[S[j]]);
if (maxNumber > maxFrequency)
{
ans++;
}
}
}
return ans;
}
static void Main()
{
string S = "123221" ;
Console.WriteLine(CountStrings(S));
}
}
|
Javascript
function GFG(S) {
const N = S.length;
let ans = 0;
for (let i = 0; i < N; i++) {
const freq = Array(10).fill(0);
let maxNumber = 0;
let maxFrequency = 0;
for (let j = i; j < Math.min(i + 72, N); j++) {
freq[parseInt(S[j])]++;
maxNumber = Math.max(maxNumber, parseInt(S[j]));
maxFrequency = Math.max(maxFrequency, freq[parseInt(S[j])]);
if (maxNumber > maxFrequency) {
ans++;
}
}
}
return ans;
}
function main() {
const S = "123221" ;
console.log(GFG(S));
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...