给定 m 个字符串,请计算有哪些字符在所有字符串中都出现过 n 次及以上。

一、题目

描述

给定 m 个字符串,请计算有哪些字符在所有字符串中都出现过 n 次及以上。

解答要求

时间限制:1000ms, 内存限制:256MB

输入

  1. 首行是整数 n ,取值范围 [1,100]
  2. 第二行是整数 m ,表示字符串的个数,取值范围 [1,100]
  3. 接下来 m行,每行一个仅由英文字母和数字组成的字符串,长度范围 [1,1000)

输出

按ASCII码升序输出所有符合要求的字符序列; 如果没有符合要求的字符,则输出空序列 [ ]。

样例
输入样例 1

2
3
aabbccFFFFx2x2
aaccddFFFFx2x2
aabcdFFFFx2x2

输出样例 1

[2 F a x]

二、代码模板

package test;
import java.nio.charset.StandardCharsets;
import java.util.*;


public class test3 {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());
        int nValue = cin.nextInt();
        int mValue = cin.nextInt();
        cin.nextLine();
        String[] strings = new String[mValue];
        for (int i = 0; i < mValue; i++) {
            strings[i] = cin.nextLine();
        }
        cin.close();

        char[] results = getNTimesCharacter(nValue, strings);

        System.out.print("[");
        for (int i = 0; i < results.length; i++) {
            if (i == 0) {
                System.out.print(results[i]);
            } else {
                System.out.print(" " + results[i]);
            }
        }
        System.out.print("]");
    }

    // 待实现函数,在此函数中填入答题代码
    private static char[] getNTimesCharacter(int nValue, String[] strings) {
        return new char[0];
    }
}

三、个人代码结果

import java.nio.charset.StandardCharsets;
import java.util.*;


public class test3 {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());
        int nValue = cin.nextInt();
        int mValue = cin.nextInt();
        cin.nextLine();
        String[] strings = new String[mValue];
        for (int i = 0; i < mValue; i++) {
            strings[i] = cin.nextLine();
        }
        cin.close();

        char[] results = getNTimesCharacter(nValue, strings);

        System.out.print("[");
        for (int i = 0; i < results.length; i++) {
            if (i == 0) {
                System.out.print(results[i]);
            } else {
                System.out.print(" " + results[i]);
            }
        }
        System.out.print("]");
    }

    // 待实现函数,在此函数中填入答题代码
    private static char[] getNTimesCharacter(int nValue, String[] strings) {
        //System.out.println("strings.length = " + strings.length);
        //将符合要求的字符存入set集合,并放入list中
        List<HashSet<Character>> list = new ArrayList<>();
        //遍历字符串数组
        for(int i = 0 ; i<strings.length;i++ ){
            //获取数组元素,字符串
            String str = strings[i];

            Map<Character,Integer> map = new HashMap<Character, Integer>();
            HashSet<Character> set = new HashSet();
            //list.add(set);
            //遍历字符串
            for(int j=0 ; j<str.length();j++){
                //将字符串字符作为K,相同字符数量作为V,存入map
                if(map.containsKey(str.charAt(j))){
                    Integer value = map.get(str.charAt(j))+1;
                    map.put(str.charAt(j),value);
                }else{
                    map.put(str.charAt(j),1);
                }
                //如果字符串字符的数量大于等于限定值,就将其放入Set中
                if(map.get(str.charAt(j)) >= nValue){
                    set.add(str.charAt(j));
                }
            }
            //将Set集合放入list
            list.add(set);
        }
        HashSet<Character> hashSet0 = list.get(0);
        Map<Character,Integer> mapEnd = new HashMap<Character, Integer>();
        //遍历第一个set集合
        hashSet0.forEach(a -> {
            //将第一个set中的元素放入map
            mapEnd.put(a,1);
            for (int i = 1; i < list.size(); i++) {
                //获取其他set集合
                HashSet<Character> hashSet1 = list.get(i);
                //如果其他set集合中存在第一个set中的元素,则元素数量加一
                if (hashSet1.contains(a))
                    mapEnd.put(a ,mapEnd.get(a)+1) ;
            }
        });

        //筛选出符合条件的字符
        List<Character> lists = new ArrayList<>();
        mapEnd.forEach((k,v)->{
            if (v==list.size()){
                lists.add(k);
            }
        });
        //将筛选出符合条件的字符放入char[]
        char[] cha = new char[lists.size()];
        for (int i = 0; i < lists.size(); i++) {
            cha[i]=lists.get(i);
        }
        return cha;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值