华为机试-报纸匿名信

题目描述: 

  • 电视剧《分界线》里面有一个片段,男主为了向警察透露案件细节,且不暴露自己,于是将报刊上的字减下来,剪拼成匿名信。
  • 现在又一名举报人,希望借鉴这种手段,使用英文报刊完成举报操作。
  • 但为了增加文竟的混淆度,只需满足每个单词中字母数量一致即可,不关注每个字母的顺序。

解释:

  1. 单词on允许通过单词no进行替代。
  2. 报纸代表newspaper,匿名信代表anonymousLetter,求报纸内容是否可以拼成匿名信。

输入描述: 

  • 第一行输入newspaper内容,包括1-N个字符串,用空格分开第二行输入anonymousLetter内容,包括1-N个字符串,用空格分开
  • newspaper和anonymousLetter的字符串由小写英文字母组成,且每个字母只能使用一次;newspaper内容中的每个字符串字母顺序可以任意调整,但必须保证字符串的完整性(每个字符串不能有多余字母)1<N < 100.
  • 1 <= newspaper.length,anonymousLetter.length <= 10^4

输出描述: 

  • 如果报纸可以拼成匿名信返回true,否则返回false

用例: 

示例一:

        输入: ab cd 

                    ab

        输出:true

示例二:

        输入: ab ef 

                    abef

        输出:false

package com.test.test_test01;

import java.util.*;

public class test_newspaper {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 报纸
        String[] str_news = in.nextLine().split(" ");
        // 匿名信
        String[] str_paper = in.nextLine().split(" ");
        // 用hashset承接每个字符串数组中的字符串,无序且不重复
        List<Set<Character>> list_news = new ArrayList<>();
        int flag = 1;
        // 如果匿名信的长度大于报纸,直接返回false
        if(str_paper.length > str_news.length){
            System.out.println(false);
        } else {
            // 存储所有的报纸中的String
            for(String index : str_news){
                Set<Character> set = new HashSet();
                char[] chr = index.toCharArray();
                for(char char_index : chr){
                    set.add(char_index);
                }
                list_news.add(set);
            }
            for(String index : str_paper){
                Set<Character> set = new HashSet();
                char[] chr = index.toCharArray();
                // 存储匿名信片段
                for(char char_index : chr){
                    set.add(char_index);
                }
                // 如果报纸中含有符合条件的匿名信片段,将符合条件的字符串(hashset)的移出list
                if(list_news.contains(set)){
                    list_news.remove(set);
                } else {
                    // 否则失败
                    System.out.println(false);
                    flag = 0;
                    break;
                }
            }

            if(flag == 1){
                System.out.println(true);
            }
        }
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值