简体   繁体   English

如何确定两个不同的单词是否具有相同的字母?

[英]How to determine if 2 different words have the same letters?

I have a word list that has all the words in the dictionary and I am supposed to use the list to find if any inputted word has the same characters as others. 我有一个单词表,其中包含字典中的所有单词,应该使用该列表查找是否有任何输入的单词与其他单词具有相同的字符。

What I tried was using the Arrays.sort() method to alphabetize every word and the input word then use the .equals() method to compare the 2 strings. 我尝试使用Arrays.sort()方法将每个单词和输入单词按字母顺序排列,然后使用.equals()方法比较两个字符串。 Then print the instance where .equals() is true. 然后打印.equals()为true的实例。

However, when I do this, the output just prints every word in the list and I am not sure why. 但是,当我这样做时,输出仅输出列表中的每个单词,我不确定为什么。

Edit for clarity : the idea is to determine the words in the list that match the input. 为清楚起见进行编辑:想法是确定列表中与输入匹配的单词。 For example: if I enter act, all words with the letters a,c, and t would be the output. 例如:如果我输入act,则所有带有字母a,c和t的单词都是输出。 In this case the only output would be cat. 在这种情况下,唯一的输出将是cat。

This question isn't really clear, but I'll provide a solution that's different from others I've seen: 这个问题目前还不清楚,但是我将提供与我所见过的其他解决方案不同的解决方案:

You can also use list removeAll to compare common letters between two lists... 您还可以使用列表removeAll比较两个列表之间的常用字母...

listA.removeAll(listB); //listA will contain any letters not common to listB
if (listA.size() == 0) {
    return true; 
}

Test output: 测试输出:

apppppppleeeeeeee <-- Input
[a, p, p, p, p, p, p, p, l, e, e, e, e, e, e, e, e] //String split 1
[a, p, p, l, e] //String split 2
letters are the same

appleeeeeef <-- Input
[a, p, p, l, e, e, e, e, e, e, f]
[a, p, p, l, e]
contains different letters

Code: 码:

public class Main {

    public Main () {
        Scanner scan;
        scan = new Scanner(System.in);
        String input = scan.nextLine(); 
        if(check(split(input), split("apple"))) {
            System.out.println("letters are the same");
        }
        else {
            System.out.println("contains different letters");
        }
    }

    public boolean check(ArrayList<String> listA, ArrayList<String> listB) {
        listA.removeAll(listB); //listA will contain any letters not common to listB
        if (listA.size() == 0) 
            return true;
        return false;
    }

    public ArrayList<String> split(String word) {
        String[] splitMe = word.split("(?!^)");
        ArrayList<String> splitList = new ArrayList<String>();
        for (int i = 0; i < splitMe.length; i++) {
            splitList.add(splitMe[i]);  
        }
        System.out.println(splitList);
        return splitList;
    }

    public static void main(String[] args) {
        Main main = new Main();
    }
}

Simple algorithm to find out if two words share the same letters: 找出两个单词是否共享相同字母的简单算法:

  1. sort the letters in each word 对每个单词中的字母进行排序
  2. alphabetize the results 按字母顺序排列结果
  3. look for duplicates 寻找重复

This only shows existence of anagrams, of course. 当然,这仅显示字谜的存在。 To return a list of anagrams is a little more work. 要返回字谜列表,还需要做更多的工作。 One way to do it would be to make a Map where your keys are the sorted words (that is, the characters in each word have been sorted) and the values are lists of words which have those letters. 一种实现方法是制作一个Map,其中您的键是排序的单词(即,每个单词中的字符都已排序),而值是包含这些字母的单词列表。

Example (code not tested, for illustration only): 示例(未经测试的代码,仅用于说明):

Map <String, List<String>> anagrams = new HashMap<String, List<String>>();
for (String s:inputList)
{
  key = sort(s); // returns s as String, chars sorted, trivial
  if (anagrams.get(key) == null)
  { 
    anagrams.put(key, new LinkedList<String>());
  }
  anagrams.get(key).add (s);
}
  String lst[] = {"asd","qwe","zxc"}; System.out.println(Arrays.asList(lst).contains("assd")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一组单词中以不同顺序搜索具有相同字母的单词 - How do I search for a word in an array of words with the same letters in a different order 在Java中,如何确定输入字符串中的单词是否以随机分配的字母开头? - How to determine if words in an input string start with random assigned letters in Java? 即使标签具有不同的值,如何确定两个XML文件是否具有相同的结构? - How to determine if two XML files have the same structure even if the tags have different values? 如何确定 object 是相同还是不同 - How to determine if an object is the same one or different 如何创建随机字母的单词? - How to create words of random letters? 选择带有至少两个不同字母的单词 - Select words with at least two different letters 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once? 如何查找单词是否彼此接近且末尾字母相同,并用逗号分隔? (JTextArea) - How to find if words are close to each and with the same ending letters, and put comma? (JTextArea) R和Java具有不同的体系结构? 我如何确定呢? - R and Java have different architectures? How do i determine that? 如何查找仅包含大写字母的单词(字符串)? - How to find words(strings) with only Uppercase letters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM