STONES - Editorial

@yyash

I believe that you don’t have to delete the characters of stone. Meaning, multiple isntances of that stone occurring has to be counted. Your code fails in the following test cases-

Input
1
abc
aaa

Output
1

Expected Output-
3
3 Likes

@yyash here’s the working code.

I modified your code a bit. PLease compare it with your original code.

And yes you don’t have to use that count array to display the results of test cases all at once. You can output the results of each test case one by one. And try using ideone next time to share codes

:smiley:

3 Likes

plz check my code as i am getting all cases right

import java.util.Scanner;
class codechef
{
public static void main(String[] args)
{
Scanner kb=new Scanner(System.in);
	int test=kb.nextInt();
	while(test-->0)
{
String s1=kb.next();
		String s2=kb.next();
		int ans=0;
		for(int i=0;i<s1.length();i++)
		{
			for(int j=i;j<s2.length();j++)
			{
				if(s1.charAt(i)==s2.charAt(j))
				ans++;
			}
		}
		System.out.println(ans);

``}

``}
`}

If anyone who doesn’t know much about hashset or any data structure , u can go do this ques by using simple brute force .U can go through my code.
https://www.codechef.com/viewsolution/15487815
If u have any issue feel free to share.

//what is the problem in this programme.
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
int main()
{
int t,i,k,a;
string s,j;
cin>>t;

while(t--)
{
	cin>>j>>s;
	int p=0;
	for(i=0;j[i]!='\0';i++)
	{
		for(k=0;s[k]!='\0';k++)
		{
			if(j[i]==s[k])
			{
				p++;
				s[k]='{';
				break;
			}
		}
	}

	cout<<p<<"\n";
}

return 0;

}

1 Like

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

int main()
{
int t;
cin>>t;
while(t–)
{
string s1,s2;
cin>>s1>>s2;
int a[128]={0};
int b[128]={0};
int x=128,l1=s1.length(),l2=s2.length();
for(int i=0;i<l1;i++)
{
a[s1[i]-’\0’]++;
}
for(int i=0;i<l2;i++)
{
b[s2[i]-’\0’]++;
}
int c=0;
bool k[128]={false};
for(int i=0;i<l2;i++)
{
if(b[s2[i]-’\0’]>0&&a[s2[i]-’\0’]>0&&!k[s2[i]-’\0’])
{
c+=b[s2[i]-’\0’];
k[s2[i]-’\0’]=true;
}
}
cout<<c<<endl;
}
return 0;
}

I tried to solve it using longest common subsequence method. I failed to realise my error. Can someone please point it out? My code is:

import java.util.*;

public class Main {
	
	
	
	public static int LongestCommonSubsequence (String str1, String str2)
	{
		int size1 = str1.length();
		int size2 = str2.length();
		
		int [] [] arr = new int [size1+1][size2+1];
		
		for(int j=0;j<size2+1;j++)
		{
			arr[0][j] = 0;
		}
		for(int i=1;i<size1+1;i++)
		{
			arr[i][0] = 0;
		}
		for(int i=1;i<size1+1;i++)
		{
			for(int j=1;j<size2+1;j++)
			{
				if(str1.charAt(i-1)==str2.charAt(j-1))
				{
					arr[i][j] = 1 + arr[i-1][j-1];
				}
				else
				{
					arr[i][j] = Math.max(arr[i-1][j], arr[i][j-1]);
				}
			}
		}
		return arr[size1][size2];
	}
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int test = sc.nextInt();
		while(test!=0)
		{
			String str1 = sc.next();
			String str2 = sc.next();
			System.out.println(LongestCommonSubsequence(str1,str2));
			test--;
		}
	}
}

stones.deleteCharAt(k);

is this line really required? Try submitting without this, I guess this seems to be the problem

1 Like

Yes, @shraeyas is right. I too don’t feel that the line is required.

Isn’t the code giving compilation error?

Its actually because of how the code is pasted here by him. I had to do corrections to resume checking…you can say I got experience of this now XD (since its a problem with >75% of codes copy pasted here)

Thank you. Misunderstood the logic of the question

Thanks even i was not considering the multiple instances of same stone in the jewel.

https://www.codechef.com/viewsolution/26114390
whats wrong in this code can anyone help me?

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

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

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
while(tc!=0){
String jewel=sc.next();
String stone=sc.next();
int count=0;
for(int i=0;i<jewel.length();i++){
for(int j=i;j<stone.length();j++){
if(jewel.charAt(i)==stone.charAt(j)){
count++;
}
}
}
System.out.println(count);
tc–;
}
}
}

Can Anyone tell me where am i going wrong…

What does it mean by ‘keeping an array/hashset for look up’ ?
Can we do it in python ?

@vijju1231
How to the output of this input will be 100 can you please explain it as there is only 1 b present in second string :slight_smile: )

All 100 characters in 1st string are in 2nd string as well.

This is that implementation. Hope you like it.

t = int(input())

while(t>0):
    j = input()
    s = input()
    j_dict = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0, 'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, 'Y': 0, 'Z': 0}
    s_dict = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0, 'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, 'Y': 0, 'Z': 0}
    
    for i in j:
        j_dict[i] += 1
    
    for i in s:
        s_dict[i] += 1
    
    count = 0
    for alpha in s_dict.keys():
        if(s_dict[alpha] >= 1):
            if(j_dict[alpha] >= 1):
                count += s_dict[alpha]
    
    # print(j_dict)
    # print(s_dict)
    print(count)
    t -= 1
1 Like

Thanks man !