计挑-C++-20-编程3-3199

题目描述
给定两个字符串str1和str2(长度均<=10000),问字符串str2内每个字符是否能在字符串str1内找到。

输入说明
第1行输入字符串str1
第2行输入字符串str2
输出说明
若能找到,则输出‘Y’,否则输出‘N’
输入样例
abdcdewrtde
wbaqx
输出样例
YYYNN

很简单,一个set就能解决的事情

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

public class Main{
  public static void main(String[] args) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    String str1 = reader.readLine();
    String str2 = reader.readLine();
    
    StringBuilder sb = new StringBuilder();
    
    Set<Character> chars = new HashSet<>();
    
    for(int i = 0;i<str1.length();i++)
      chars.add(str1.charAt(i));
    
    for(int i=0;i<str2.length();i++){
      if(chars.contains(str2.charAt(i))) sb.append("Y");
      else sb.append("N");
    }
    
    System.out.println(sb);
  }
}
posted @ 2022-11-29 22:19  YaosGHC  阅读(55)  评论(0)    收藏  举报