Jewels ans Stones | CodeChef Solution

Hello coders, today we are going to solve Jewels and Stones CodeChef Solution whose Problem Code is STONES.

Jewels ans Stones

Contents

Task

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood – now she has become really good with identifying which ones are fake and which ones are not. Her King requested for her help in mining precious stones, so she has told him which all stones are jewels and which are not. Given her description, your task is to count the number of jewel stones.

More formally, you’re given a string J composed of latin characters where each character is a jewel. You’re also given a string S composed of latin characters where each character is a mined stone. You have to find out how many characters of S are in J as well.

Input Format

First line contains an integer T denoting the number of test cases. Then follow T test cases. Each test case consists of two lines, each of which contains a string composed of English lower case and upper characters. First of these is the jewel string J and the second one is stone string S.
You can assume that 1 <= T <= 100, 1 <= |J|, |S| <= 100

Output Format

Output for each test case, a single integer, the number of jewels mined.

Example

Sample Input

4
abc
abcdef
aA
abAZ
aaa
a
what
none

Sample Output

3
2
1
0

Solution – Jewels and Stones

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string str, str1;
        cin>>str>>str1;
        int n=str1.length();
        int m=str.length();
        int sum=0;
        for(int i=0; i<n; i++)
        {
            int a=0;
            for(int j=0; j<m; j++)
            {
                if(str[j]==str1[i])
                {
                    a++;
                    break;
                }
            }
            if(a>0)
                sum++;
        }
        cout<<sum<<endl;
    }
}

Python

# cook your dish here
T = int(input())
for x in range(T):
    j = list(input())
    s = list(input())
    
    k = 0
    for i in s:
        if i in j:
            k+=1
        else:
            continue
    print(k)

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
      try {
		    Scanner sc = new Scanner(System.in);
		    int loop = sc.nextInt();
		    while (loop-- > 0) {
		    String s = sc.next();
		    String j = sc.next();
		    int count = 0;
		    for(int i = 0; i<j.length(); i++){
		        if(s.contains(Character.toString(j.charAt(i)))){
		            count++;
		        }
		    }
            System.out.println(count);
		    }
		} catch (Exception e) {
		    
		}	}
}

Disclaimer: The above Problem (Jewels ans Stones) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.