给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符

给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序
返回答案。
示例 1:
输入:words = ["bella","label","roller"]
输出:["e","l","l"]
示例 2:
输入:words = ["cool","lock","cook"]
输出:["c","o"]
提示:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] 由小写英文字母组成

方法一:**错误,有问题“

  	   String[] words = {"bella","label","roller"};

	   // 将字符串 以字母为下标,以出现的次数为值存储,
	   // 重复的字符肯定是
       HashMap<Character,Integer> ret = new HashMap<Character,Integer>();
       int count = 0;
       for(int i=0; i< words.length;++i) {
           for (int j = 0; j < words[i].length(); ++j) {
               char key = words[i].charAt(j);

                   if (ret.containsKey(key)) {
                       count = ret.get(key);
                       count++;
                   } else {
                       count = 1;
                   }
                    ret.put(key,count);
                }
       }

       for (Character character : ret.keySet()){
           count = ret.get(character);
               int j = 0;
               while( j < count/words.length ){
                   System.out.print( character+ " " );
                   j++;
               }
       }

方法二:

import java.util.Arrays;

public class Main {
    /*
        给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序
        返回答案。
        示例 1:
        输入:words = ["bella","label","roller"]
        输出:["e","l","l"]
        示例 2:
        输入:words = ["cool","lock","cook"]
        输出:["c","o"]
        提示:
        1 <= words.length <= 100
        1 <= words[i].length <= 100
        words[i] 由小写英文字母组成
     */
    public static void main(String[] args) {
     	String[] words = {"bella","label","roller"};
    	//定义一个26位的数组,
        int ret[] = new  int[26];
        //假设每个字母都是重复的,所以每个字符对应的值都是最多的
        Arrays.fill(ret,Integer.MAX_VALUE);
        for(int i = 0; i < words.length; i++){
        	// 为每一个字符串准备一个数组,
            int[] hash = new int[26];
            for(int j = 0; j<words[i].length(); j++){
            	//将对应字母脚标的数组加加(代表这个字符在此字符串出现的次数)
                char ch = words[i].charAt(j);
                ++hash[ch-'a'];
            }

            for(int j = 0; j < 26; j++) {
            	/* 将每一个字符串的数组 与 上一次的结果比较取最小的值。 
            		如果有一个字串没有此字符(用0代表),后续的字符串此字母不可能重复
            		如果多个字符串包含相同字母,只能取做小数的那个字符串
         			所以将每一个字符串对应的结果集做比较,获取到的最终数组,大于0 就是重复字母的次数, 下表就是字符
            		例如 ellp 和 olw , 
            		[e]=1 [o]=0 [l]=2 [p]=1 [w]=0
            		[e]=0 [o]=1 [l]=1 [p]=0 [w]=1
            		两个取最小值结果如下
            		[e]=0 [o]=0 [l]=1 [p]=0 [w]=0
            		所以重复字符就是 l 重复次数是 1
            	*/
                ret[j] = Math.min(ret[j], hash[j]);

            }
        }

        for(int i = 0; i < 26; i++)
            if (ret[i] > 0) {
                int index = i + 'a';
                char c = (char) index;
                int j = 0;
                while(j < ret[i]) {
                    System.out.print(c + " " );
                    j++;
                }
            }

    }
}

方法3:

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

public class Main {
    /*
        给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序
        返回答案。
        示例 1:
        输入:words = ["bella","label","roller"]
        输出:["e","l","l"]
        示例 2:
        输入:words = ["cool","lock","cook"]
        输出:["c","o"]
        提示:
        1 <= words.length <= 100
        1 <= words[i].length <= 100
        words[i] 由小写英文字母组成
     */
    public static void main(String[] args) {
        String[] words = {"bella","label","roller"};
        HashMap<Character,Integer> ret = new HashMap<Character,Integer>();

        int total = 1;
        for(int i = 0; i < words.length; i++) {
            HashMap<Character,Integer> tmp = new HashMap<Character,Integer>();
            for (int j = 0; j < words[i].length(); ++j) {

                char key = words[i].charAt(j);
                if(i == 0){
                    if(tmp.containsKey(key)){
                        total=tmp.get(key);
                        total++;
                        tmp.put(key,total);
                    }else {
                        tmp.put(key, total);
                    }

                }
                if(i > 0){
                    if(ret.containsKey(key)){
                       if(tmp.containsKey(key)){
                           int tmp1 = ret.get(key);
                           int tmp2 = tmp.get(key);
                           if(tmp1  > tmp2 ){
                               tmp2++;
                               tmp.put(key,tmp2);
                           }else{
                               tmp.put(key,tmp1);
                           }
                       }else {
                           tmp.put(key, 1);
                       }
                    }
                }


            }
            ret.clear();
            ret.putAll(tmp);
            tmp.clear();
        }


        Set<Map.Entry<Character, Integer>> entries = ret.entrySet();
        for (Map.Entry<Character, Integer> entry: entries) {
            for(int i = 0; i < ret.get(entry.getKey()); i++ ){
                System.out.print(entry.getKey()+" ");
            }

        }


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值