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

字符串字符匹配

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

用Set走捷径!把短字符串的字符加入到Set中,把长字符的字符从Set中剔除,如果最后Set为空,则满足条件。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            String shortstr = in.nextLine();
            String longstr = in.nextLine();
            Set<Character> set = new HashSet<>();
            for(int i=0;i<shortstr.length();++i){
                set.add(shortstr.charAt(i));
            }
            for(int i=0;i<longstr.length();++i){
                set.remove(longstr.charAt(i));
            }
            System.out.println(set.size()==0);
        }
    }
}
全部评论
这道题和HASH 有什么关系么
点赞 回复 分享
发布于 2024-09-13 21:07 北京
你这个虽然能过测试:但是我发现一个问题,当短串:ia, 长串:iba ;返回的是true
点赞 回复 分享
发布于 2023-02-06 22:58 四川

相关推荐

11-13 14:37
门头沟学院 Java
点赞 评论 收藏
分享
评论
30
5
分享

创作者周榜

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