【解決方法】この問題を解決できません。


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'.

私が試したこと:

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));
    }
  }
}

解決策 1

あなたのコードはそれを何もしません – あなたの家庭教師があなたに提供したものは、チェックする文字と文字列を設定しますが、「最良の文字列」を見つけようとしません。

一度に 1 つのことを行うようにコードを単純化します。文字列と一連の文字を受け取り、それが「最適な」文字列かどうかをチェックする関数を作成します。 そうである場合は true を返し、そうでない場合は false を返します。

さまざまな数の文字列と文字セットを使用して、単独でテストします。 機能したら、セット内の文字列ごとに呼び出して数を数えます。

これは役立つかもしれません: 問題を解決するためのコードの書き方、初心者向けガイド[^]

コメント

タイトルとURLをコピーしました