java题目 字符串字符匹配

描述

判断短字符串S中的所有字符是否在长字符串T中全部出现。
请注意本题有多组样例输入。
数据范围:1\le len(S),len(T)\le200\1len(S),len(T)200 
进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(n)\O(n) 

输入描述:

输入两个字符串。第一个为短字符串,第二个为长字符串。两个字符串均由小写字母组成。

输出描述:

如果短字符串的所有字符均在长字符串中出现过,则输出字符串"true"。否则输出字符串"false"。

示例1

输入:
bc
abc
apgmlivuembu
tyjmrcuneguxmsqwjslqvfmw
bca
abc
输出:
true
false
true
说明:
第一组样例:
bc
abc
其中abc含有bc,输出"true"
第二组样例,上面短字符串的a就没有在下面长字符串出现,输出"false"  

 

 

 1 import java.io.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5     public static void main(String[] args) throws IOException {
 6         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 7         String str1="";
 8         String str2="";
 9         while((str1 = br.readLine()) != null) {
10             str2 = br.readLine();
11             char[] ch1 = str1.toCharArray();  // 短字符串
12             boolean result = true;
13             for(char c : ch1) {
14                 if( !str2.contains(String.valueOf(c))){  // 长字符串不包含短字符串中任何一个字符,则返回false
15                     result = false;
16                     break;
17                 }
18             }
19             System.out.println(result);
20         }
21     }
22 }

 

posted @ 2022-03-06 11:33  海漠  阅读(219)  评论(0)    收藏  举报