题解 | #字符串字符匹配#

字符串字符匹配

https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    /**
     * 判断短字符串S中的所有字符是否在长字符串T中全部出现。
     * @param str1  短字符串
     * @param str2  长字符串
     */
    public static boolean occurs(String str1, String str2) {
        Set<Character> set1 = new HashSet<>();
        Set<Character> set2 = new HashSet<>();
        for (int i = 0; i < str1.length(); i++) {
            set1.add(str1.charAt(i));
        }
        for (int i = 0; i < str2.length(); i++) {
            set2.add(str2.charAt(i));
        }
        for (Character ch : set1) {
            if (!set2.contains(ch)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();
        boolean flag = occurs(s1, s2);
        System.out.println(flag);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-24 13:36
点赞 评论 收藏
分享
06-07 17:17
嘉兴学院 教师
心爱的idea:你孩
点赞 评论 收藏
分享
07-16 18:03
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务