Check whether count of distinct characters in a string is Prime or not
Given a string of lowercase English alphabets. The task is to check if the count of distinct characters in the string is prime or not.
Examples:
Input : str = "geeksforgeeks"
Output :Yes
Explanation: The number of distinct characters in the
string is 7, and 7 is a prime number.
Input : str ="geeks"
Output : No
In this problem first we have to count the distinct characters in the string. We will use a map to store the frequency of each alphabets. The next step is to count the number of distinct characters, and check whether the number is prime or not .
If the number is prime we will print Yes, else No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
int i;
if (n == 1)
return false ;
for (i = 2; i <= sqrt (n); i++) {
if (n % i == 0)
return false ;
}
return true ;
}
int countDistinct(string s)
{
unordered_map< char , int > m;
for ( int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
return m.size();
}
int main()
{
string str = "geeksforgeeks" ;
if (isPrime(countDistinct(str)))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isPrime( int n)
{
int i;
if (n == 1 )
return false ;
for (i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
return false ;
}
return true ;
}
static int countDistinct(String s)
{
Set<Character> m = new HashSet<Character>();
for ( int i = 0 ; i < s.length(); i++)
{
m.add(s.charAt(i));
}
return m.size();
}
public static void main(String []args)
{
String str = "geeksforgeeks" ;
if (isPrime(countDistinct(str)))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import sqrt
def isPrime(n) :
if n = = 1 :
return False
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if n % i = = 0 :
return False
return True
def countDistinct(s) :
m = {}
m = m.fromkeys(s, 0 )
for i in range ( len (s)) :
m[s[i]] + = 1
return len (m.keys())
if __name__ = = "__main__" :
str = "geeksforgeeks"
if isPrime(countDistinct( str )) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isPrime( int n)
{
int i;
if (n == 1)
return false ;
for (i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
static int countDistinct(String s)
{
HashSet< char > m = new HashSet< char >();
for ( int i = 0; i < s.Length; i++)
{
m.Add(s[i]);
}
return m.Count;
}
public static void Main(String []args)
{
String str = "geeksforgeeks" ;
if (isPrime(countDistinct(str)))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPrime(n)
{
var i;
if (n == 1)
return false ;
for (i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false ;
}
return true ;
}
function countDistinct(s)
{
var m = new Map();
for ( var i = 0; i < s.length; i++) {
if (m.has(s[i]))
{
m.set(s[i], m[s[i]]+1);
}
else
{
m.set(s[i],1);
}
}
return m.size;
}
var str = "geeksforgeeks" ;
if (isPrime(countDistinct(str)))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O((len(str))1/2)
- Auxiliary Space: O(len(str))
Method 2: Using Counter function:
- Count the frequencies of all elements using Counter function and number of keys of this frequency dictionary gives the count and check whether it is prime or not.
Below is the implementation:
C++
#include <iostream>
#include <unordered_map>
#include <cmath>
using namespace std;
bool isPrime( int n) {
if (n == 1) {
return false ;
}
for ( int i = 2; i <= sqrt (n); i++) {
if (n % i == 0) {
return false ;
}
}
return true ;
}
bool countDis(string str) {
unordered_map< char , int > freq;
for ( int i = 0; i < str.length(); i++) {
char ch = str[i];
freq[ch]++;
}
if (isPrime(freq.size())) {
return true ;
} else {
return false ;
}
}
int main() {
string S = "geeksforgeeks" ;
cout << countDis(S) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean isPrime( int n) {
if (n == 1 ) {
return false ;
}
for ( int i = 2 ; i <= Math.sqrt(n); i++) {
if (n % i == 0 ) {
return false ;
}
}
return true ;
}
public static boolean countDis(String str) {
Map<Character, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < str.length(); i++) {
char ch = str.charAt(i);
freq.put(ch, freq.getOrDefault(ch, 0 ) + 1 );
}
if (isPrime(freq.keySet().size())) {
return true ;
} else {
return false ;
}
}
public static void main(String[] args) {
String S = "geeksforgeeks" ;
System.out.println(countDis(S));
}
}
|
Python3
from collections import Counter
from math import sqrt as sqrt
def isPrime(n):
if n = = 1 :
return False
for i in range ( 2 , int (sqrt(n)) + 1 ):
if n % i = = 0 :
return False
return True
def countDis( str ):
freq = Counter( str )
if (isPrime( len (freq))):
return True
else :
return False
if __name__ = = "__main__" :
S = "geeksforgeeks"
print (countDis(S))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Gfg
{
public static bool isPrime( int n)
{
if (n == 1)
{
return false ;
}
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
public static bool countDis( string str)
{
Dictionary< char , int > freq = new Dictionary< char , int >();
foreach ( char ch in str)
{
if (freq.ContainsKey(ch))
{
freq[ch]++;
}
else
{
freq.Add(ch, 1);
}
}
if (isPrime(freq.Count))
{
return true ;
}
else
{
return false ;
}
}
public static void Main()
{
string S = "geeksforgeeks" ;
Console.WriteLine(countDis(S));
}
}
|
Javascript
function isPrime(n) {
if (n === 1) {
return false ;
}
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
return false ;
}
}
return true ;
}
function countDis(str)
{
let freq = {};
for (let i = 0; i < str.length; i++) {
freq[str[i]] = (freq[str[i]] || 0) + 1;
}
if (isPrime(Object.keys(freq).length)) {
return true ;
} else {
return false ;
}
}
let S = "geeksforgeeks" ;
console.log(countDis(S));
|
- Time Complexity: O((len(str))1/2)
- Auxiliary Space: O(len(str))
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...