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

字符串字符匹配

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

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while(in.hasNextLine()){
            String shortS = in.nextLine();
            String longS = in.nextLine();

            Map<Character, Integer> hashmap = new HashMap<>();

            for(int i=0 ; i<shortS.length() ; ++i){
                hashmap.put(shortS.charAt(i), hashmap.getOrDefault(shortS.charAt(i),0)+1);
            }

            //System.out.println(hashmap);

            for(int i=0 ; i<longS.length() ; ++i){
                char c=longS.charAt(i);
                if(hashmap.containsKey(c)){
                    hashmap.put(c, 0);
                }
                
            }
            boolean flag = true;
            for(Character key:hashmap.keySet()){
                if(hashmap.get(key)!=0){
                    flag = false;
                    break;
                }
            }
            System.out.println(flag);
        }
    }
}

全部评论

相关推荐

牛客62533758...:华为不卡双非,而是卡院校hhhh
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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