Count prime valued strings from set bits of number sums

Updated

Problem Statement :

Given N strings as input. Each string is made up of alphanumeric characters and consists of lowercase characters [a,z]  and numbers in range[1,999] (eg- “ac67rt349iu2td1yt98z”  in this string the numbers are 67,349,2,1,98). Now our task is to find all those numbers and take the product of the sum of odd numbers and sum of even numbers from those numbers  then find the count of set bits of binary form of the product we got (this count is value of string). Lastly find the count of strings whose value is prime and not prime. Output “WIN” if  prime valued string is more or “LOSE” if prime valued string are less and “DRAW” if both (prime valued and non prime valued string count ) are same.  (Note-if a string does not have any number then its value is1  or if numbers are only odd or only even then we take  just that sum)

1<=N<=1000

1<=length(String)<=100

Example :

Input :
N=5
"ab23nf67jj2o24"
"abcderth"
"jf367hcrt2kv4"
"a2h4j6"
"qy3uh2jg1"
Output : "LOSE"
Explanation :
We have 5 strings, values of each will be-
"ab23nf67jj2o24"- numbers [23,67,2,24] even sum=2+24=26, odd
sum=23+67=90 and string value=26*90=2340, Binary form =100100100100
"abcderth"- no number exist so string value=1, binary form =1
"jf367hcrt2kv4"- numbers [367,2,4] even sum=2+4=6 , odd sum=367 and
string value=6*367=2202, binary form=100010011010
"a2h4j6"- numbers [2,4,6] even sum=2+4+6=12 , odd
sum=0(no odd number exist) and string value=12,binary form=1100
"qy3uh2jg1"- numbers [3,2,1] even sum=2 ,odd sum=3+1=4 and string
value=2*4=8, binary form =1000
Therefore we have 2 Prime Valued String 3rd and 4th string with set
bits 5 and 2 respectively and 3 Non Prime Valued String so in  this
case the Output will be "LOSE".

Input :
N=2
"abcdef"
"avd2hg4"
Output : "DRAW"
Explanation :
We have 2 strings,values of each will be-
"abcdef"- no number exist so string value=1, binary form =1
"avd2hg4"-numbers [2,4] even sum=2+4=6 , odd
sum=0(no odd number exist) and string value=6,binary form=110
Therefore we have 1 Prime Valued String "avd2hg4" with set bits 2 and
we have 1 Non Prime Valued String so in this case the Output will be
"DRAW".

Approach :

Observation- if we focus on the length of binary string then it will not go above the range of 60, so we will find   all prime numbers from 1 to 60 and store it for use  or we can check for each number for prime it will be processes within time.

Now we will follow these steps:

  1. We will first traverse through each string and find all the numbers in each string, then we will proceed according to the question to find the string values.
  2. We will use a function to find convert the string value passed to it into binary and then check if it is Prime Valued .
  3. We keep the count of Prime and Non Prime valued string and according output “WIN”/”LOSE”/”DRAW”.
Six steps for the string jf367hcrt2kv4 from its numbers to the product 2202, 5 set bits, a prime test and the final LOSE.

Implementation :

import string
import math
#function to check primality
def chkPrime(n):
  #base condition
  if n <= 1:
    return False
  #formula
  max_div = math.floor(math.sqrt(n))
  for i in range(2, 1 + max_div):
    if n % i == 0:
      return False
  return True
    
    
#function to check prime valued string
def chkPVS(num):
    #store directly the count of set bits
    cnt=(bin(num)[2:]).count("1")
    if(chkPrime(cnt)):
        return True
    else:
        return False

#function to find the string values
def StrVal(s):
    #list to store all numbers
    l=[]
    #initialize counters
    odd_sum=0
    even_sum=0
    a=0
    #traverse to get all numbers
    for i in range(len(s)):
        if(s[i].isdigit()):
            a=a*10+int(s[i])
        else:
            #if the next character is not digit
            #then store it in l
            if(i==0):
                continue
            elif(s[i-1].isdigit()):
                l.append(a)
                a=0
    #edge case if the number is at last
    if(s[-1].isdigit()):
        l.append(a)
    #taking odd and even sum
    for i in l:
        if(i%2==0):
            even_sum+=i
        else:
            odd_sum+=i
    val=0
    #cases when any sum value is 0
    #finding the string value
    if(even_sum==0 and odd_sum!=0):
        val=odd_sum
    elif(even_sum!=0 and odd_sum==0):
        val=even_sum
    elif(even_sum==0 and odd_sum==0):
        val=1
    else:
        val=even_sum*odd_sum
    return val
            

#Driver code
N=int(input())
#initializing counters
prime_cnt=0
non_prime_cnt=0
#check for each string
for i in range(N):
    s=input()
    val=StrVal(s)
    #checking for prime valued string
    if(chkPVS(val)):
        prime_cnt+=1
    else:
        non_prime_cnt+=1
#checking for the WIN/LOSE/DRAW conditions
if(prime_cnt>non_prime_cnt):
    print("WIN")
elif(prime_cnt<non_prime_cnt):
    print("LOSE")
else:
    print("DRAW")
    

TIME COMPLEXITY- O(N*100)

Comment
Mithlesh Upadhyay Published 11 Nov 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials