Count even indices of String whose prefix has prime number of distinct Characters
Last Updated :
17 Apr, 2023
Given a string S of size N. The task is to find the number of Invalid characters. Index i (0 ? i < N) is invalid if i is even and the total count of distinct characters in the index range [0, i] is a prime number.
Examples:
Input: N = 6, S = “aabagh”
Output: 2
Explanation: Characters at index 2 and 4 are invalid as 2 and 4 both are even and count of distinct characters upto index 2 and 4 are 2 and 3 respectively which is prime.
Input: N = 2, S = “gg”
Output: 0
Explanation: No invalid character
Approach: This problem can be solved using the prefix array concept.
Idea: The idea is to precompute all the prime numbers in the given range of N and then just check for the required conditions at every character.
Follow the below steps to solve the problem:
- Create a precompute function and calculate all prime factors using the sieve of Eratosthenes.
- Create a hashmap to store frequencies of characters which will help us determine if the character is a duplicate or not.
- Iterate over the string from and when any even index is reached check the following:
- The number of distinct characters in the prefix is prime
- If it is true, then incremented the ans by 1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int check = 0;
int isPrime[100001];
void pre()
{
check = 1;
memset (isPrime, 1, sizeof isPrime);
isPrime[0] = isPrime[1] = 0;
for ( int i = 4; i <= 100000; i += 2)
isPrime[i] = 0;
for ( int i = 3; i * i <= 100000; i += 2) {
if (isPrime[i]) {
for ( int j = 3; j * i <= 100000; j += 2)
isPrime[i * j] = 0;
}
}
}
int solve( int N, string S)
{
if (!check)
pre();
int dis = 0;
int ans = 0;
unordered_map< char , int > f;
for ( int i = 0; i < N; i++) {
if (f[S[i]] == 0) {
f[S[i]]++;
dis++;
}
if (((i % 2) == 0) and isPrime[dis]) {
ans++;
}
}
return ans;
}
int main()
{
int N = 6;
string S = "aabagh" ;
cout << solve(N, S) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int check = 0 ;
static int [] isPrime = new int [ 100001 ];
static void pre()
{
check = 1 ;
Arrays.fill(isPrime, 1 );
isPrime[ 0 ] = isPrime[ 1 ] = 0 ;
for ( int i = 4 ; i <= 100000 ; i += 2 )
isPrime[i] = 0 ;
for ( int i = 3 ; i * i <= 100000 ; i += 2 ) {
if (isPrime[i] == 1 ) {
for ( int j = 3 ; j * i <= 100000 ; j += 2 )
isPrime[i * j] = 0 ;
}
}
}
static int solve( int N, String S)
{
if (check == 0 ) {
pre();
}
int dis = 0 ;
int ans = 0 ;
HashMap<Character, Integer> f = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
if (!f.containsKey(S.charAt(i))) {
f.put(S.charAt(i), 1 );
dis++;
}
if ((i % 2 == 0 ) && (isPrime[dis] == 1 )) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 6 ;
String S = "aabagh" ;
System.out.println(solve(N, S));
}
}
|
Python3
import math
check = 0
isPrime = []
for i in range ( 0 , 100001 ):
isPrime.append( 0 )
def pre():
check = 1
for i in range ( 0 , 100001 ):
isPrime[i] = 1
isPrime[ 0 ] = isPrime[ 1 ] = 0
for i in range ( 4 , 100001 , + 2 ):
isPrime[i] = 0
for i in range ( 3 , int (math.sqrt( 100000 )) + 1 , + 2 ):
if (isPrime[i] = = 1 ):
for j in range ( 3 , int ( 10000 / i) + 1 , + 2 ):
isPrime[i * j] = 0
def solve(N, S):
if (check = = 0 ):
pre()
dis = 0
ans = 0
f = {}
for i in range ( 0 , N):
if S[i] not in f.keys():
f[S[i]] = 1
dis + = 1
if ((i % 2 = = 0 ) and (isPrime[dis] = = 1 )):
ans + = 1
return ans
N = 6
S = "aabagh"
print (solve(N, S))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int check = 0;
static int [] isPrime = new int [100001];
static void pre()
{
check = 1;
Array.Fill(isPrime, 1);
isPrime[0] = isPrime[1] = 0;
for ( int i = 4; i <= 100000; i += 2)
isPrime[i] = 0;
for ( int i = 3; i * i <= 100000; i += 2) {
if (isPrime[i] == 1) {
for ( int j = 3; j * i <= 100000; j += 2)
isPrime[i * j] = 0;
}
}
}
static int solve( int N, string S)
{
if (check == 0) {
pre();
}
int dis = 0;
int ans = 0;
Dictionary< char , int > f
= new Dictionary< char , int >();
for ( int i = 0; i < N; i++) {
if (!f.ContainsKey(S[i])) {
f.Add(S[i], 1);
dis++;
}
if ((i % 2 == 0) && (isPrime[dis] == 1)) {
ans++;
}
}
return ans;
}
public static void Main()
{
int N = 6;
string S = "aabagh" ;
Console.WriteLine(solve(N, S));
}
}
|
Javascript
let check = 0;
let isPrime = new Array(100001).fill(1);
const pre = () => {
check = 1;
isPrime[0] = isPrime[1] = 0;
for (let i = 4; i <= 100000; i += 2)
isPrime[i] = 0;
for (let i = 3; i * i <= 100000; i += 2) {
if (isPrime[i]) {
for (let j = 3; j * i <= 100000; j += 2)
isPrime[i * j] = 0;
}
}
}
const solve = (N, S) => {
if (!check)
pre();
let dis = 0;
let ans = 0;
let f = {};
for (let i = 0; i < N; i++) {
if (!(S[i] in f)) {
f[S[i]] = 1;
dis++;
}
if (((i % 2) == 0) && isPrime[dis]) {
ans++;
}
}
return ans;
}
let N = 6;
let S = "aabagh" ;
console.log(solve(N, S));
|
Time Complexity: O(N) as pre function is only need to call one time and through which we can solve millions of string so from pre() time is O(1) and solve() travel N time and hasmap give output in O(1) in average case so overall complexity is O(N)
Auxiliary Space: O(N)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...