Count even indices of String whose prefix has prime number of distinct Characters – GeeksforGeeks

Improve Article

Save Article

Like Article

Improve Article

Save Article

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 distict 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;

}

Time Complexity: O(N√N)
Auxiliary Space: O(N)

Related Articles:

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
CharacterscountDistinctfineradar updateFree Fire Redeem Codesgadget updateGeeksforGeeksindicesLatest tech newsnumberprefixprimestringTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment