[英]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: 找出两个单词是否共享相同字母的简单算法:
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.