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

字符串字符匹配

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

利用 HashSet 解决

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意这里的 hasNext 和 hasNextLine
        while (in.hasNext()) {
            // 短串
            String S = in.nextLine();
            // 长串
            String T = in.nextLine();
            // 遍历短串,然后把他们都装入 set
            char[] Schars = S.toCharArray();
            HashSet<Character> set = new HashSet<>();
            for (char c : Schars) {
                set.add(c);
            }

            // 遍历长串,把他们都取出来
            char[] Tchars = T.toCharArray();
            for (char c : Tchars) {
                set.remove(c);
            }
            
            // 如果为空说明短串里面的都被取出来了,都在长串里
            System.out.println(set.isEmpty());
        }
    }
}

全部评论

相关推荐

程序员鼓励师阿欢:哈哈哈哈哈笑死我了😂
点赞 评论 收藏
分享
03-19 10:07
已编辑
广东药科大学 golang
Yki_:你倒是进一个面啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务