对逗号分隔的一组单词根据无重复单词,单个单词无重复字母,单词错位相同。

"opt", "max", "tpo", "hi", "tpo", "edih", "hide", "ih", "pot", "too", "aaa", "mxa", "abc"

    public static void main(String[] args) {
        String[] arr1 = {"opt", "max", "tpo", "hi", "tpo", "edih", "hide", "ih", "pot", "too", "aaa", "mxa", "abc"};
        //利用set不可重的特性
        Set<String> set1 = new HashSet<String>();
        for (int i=0;i<arr1.length;i++){
            set1.add(arr1[i]);
        }
        //分隔单词成字母,循环删值
        List<String> list1 = new ArrayList<String>(set1);
        for (String str1:list1) {
            String[] arr2 = str1.split("");
            Set<String> set2 = new HashSet<String>();
            for (int i=0;i<arr2.length;i++){
                set2.add(arr2[i]);
            }
            if(set2.size() != str1.length()){
                set1.remove(str1);
            }

        }

        //利用Arrays.sort排序来判断错误相同单词
        Set set = new HashSet();
        for (String str2:set1){
            List<String> list2 = new ArrayList<String>();
            String[] arr2 = str2.split("");
            Arrays.sort(arr2);
            for (String str3:set1){
                String[] arr3 = str3.split("");
                Arrays.sort(arr3);
                if(Arrays.equals(arr2,arr3)){
                    list2.add(str3);
                }
            }
            set.add(list2);
        }

        System.out.println(set);

    }

猜你喜欢

转载自blog.csdn.net/qq_43690617/article/details/88602827