Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
given a set 'S' of distinct characters and an array 'A' of 'N' strings.

A String in array 'A' is called best if all the characters of the string is present in the set 'S'.

You have to find how many strings in the array 'A' are best.

Input Format
The first line contains the number of test cases.

For each test case: The first line contains a string denoting the characters of the set 'S'.

The next line contains 'N', the number of strings in 'A'.

The next 'N' lines contains a string each, which are the elements of the array 'A'.

Output Format
For each test case print the count of best strings in a new line.

Example 1
Input:

1
abc
4
ab
abd
cacb
cabef
Output:

2
Explanation:

Only 'ab' and 'cacb' are best strings. Rest of the strings contain characters apart from 'abc'.

What I have tried:

import java.util.*;

public class Main {

  static int bestStrings(String s, String[] A, int n) {
    // your code here

	  for(int i=0; i<n; i++){
		  for(int j=0; j<s.length(); j++){
			  for(int k=0; k<n; k++){
				  if(A[j]==A[k])
			  }
		  }
	  }
	  
  }

  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t-- > 0) {
      String s = sc.next();
      int n = sc.nextInt();
      String[] A = new String[n];
      for (int i = 0; i < n; i++) {
        A[i] = sc.next();
      }

      System.out.println(bestStrings(s, A, n));
    }
  }
}
Posted
Updated 16-Mar-23 5:01am
v2
Comments
Richard MacCutchan 10-Feb-23 11:26am    
You forgot to ask a question.

Your code doesn't do any of that - the stuff your tutor provided you does set the characters and the strings to check, but it doesn't try to locate a "best string".

Simplify your code to do one thing at a time: Write a function that takes a string and a set of characters and it checks if it is a "best" string. If it is, return true, if it isn't, return false.

Test it in isolation with a varied number of strings and character sets. When it works, call it for each string in your set and count the number.

This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
CPallini 10-Feb-23 13:51pm    
5.
You should rewrirte your beststrings function to accept only the test sequence and a single string, rather than the array and its length. The caller should then call it once for each string in the array.
The actual algorithm for testing the string should then be:
Set count = 0
FOR EACH character c in string A:
    FOR EACH character x in string S:
        IF c EQUALS x:
            ADD 1 TO COUNT // this character exists in the test sequence
            BREAK // no need to test the others
    END FOR // tested all characters in S
END FOR // tested each character of A
IF count EQUALS A.length(): // every character matched one of S
    PRINT A + "is a best string"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900