Prime String | Practice | GeeksforGeeks

Provided a String of length N, your task is to find out whether or not the given string is a prime string. A prime string is a string in which the sum of the ASCII value of all the characters is prime.
Example 1:
Input: N = 13, S = "geeksforgeeks"
Output: YES
Explanation: As sum of ASCII is 1381,
which is prime.
â€‹Example 2: 
Input: India
Output: NO
Explanation: As sum of ASCII is 485,
which is not prime.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function isPrimeString() which takes the length of string and the string as inputs and returns true if that is a prime string, else false.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ |S| ≤ 106
0
prasadchaskar2 months ago
Python Code:
def isPrimeString(self, s):
        su = 0
        for i in s:
            su += ord(i)
        if su>1:
            for i in range(2,su):
                if su % i == 0:
                    return False
            return True
        return False
0
ayazmrz982 months ago
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 boolean isPrimeString(String s)
       {
           int sum=0;
           for(int i=0;i<s.length();i++)
           {
             sum = sum + (int)s.charAt(i);
           }
           
           return  isprime(sum);
       }
0
ankitparashxr5 months ago
class Solution{   public boolean isPrimeString(String s)       {           int count = 0;           int sum = 0;           for(int i =0;i<s.length();i++)           {               int j = s.charAt(i);               sum = sum+j;           }           for(int div = 2;div*div<=sum;div++)           {               if(sum%div==0)               {                   count++;                   break;               }           }           if(count==0)           {               return true;           }           else           {               return false;           }       }}
0
dreamvivek65 months ago
int sum=0;        for(int i=0; i<n; i++)    sum+=str[i];       for(int i=2; i<=sqrt(sum); i++)    if(sum%i==0) return false;        return true;
0
badgujarsachin837 months ago
class Solution
{
  public:
  bool prime(int n){
      if(n==1||n==0){
          return false;
      }
      for(int i=2;i<=sqrt(n);i++){
          if(n%i==0){
              return false;
          }
      }
      return true;
  }
    bool isPrimeString(int N, string str)
    {
        int sum=0;
    	//code here.
    	for(int i=0;i<N;i++){
    	    sum+=int(str[i]);
    	}
    	if(prime(sum)==true){
    	    return true;
    	}else{
    	    return false;
    	}
    }
};
0
mahendra9 months ago
mahendra
13geeeksforgeeks
this test case is wrong as the spelling is incorrect correct output should be YES as it is prime  ascii sum  1482
0
Rohit Pokhriyal9 months ago
Rohit Pokhriyal
for Java: https://uploads.disquscdn.c...
-1
Kumar Tejaswi10 months ago
Kumar Tejaswi
class Solution{  public:
    bool isprime(int n)    {        if(n<2){            return false;        }        for(int i=2;i*i<=n;i++)        {            if(n%i==0){                return false;            }        }return true;    }
    bool isPrimeString(int N, string str)    {    //code here.    int sum=0;    for(int i=0;i<n;i++){ sum+="int(str[i]);" }="" if(isprime(sum)){="" return="" true;="" }return="" false;="" }="" };="">
0
ArBoss
This comment was deleted.
0
ArBoss
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.