java - 有没有办法在搜索字谜时保留单词的大小写?

标签 java anagram

我需要编写一个程序,将整个文本文件读入字符串并在其中搜索字谜。输出必须是同一类型的所有字谜词,位于单独的行中,并带有原始的大小写书写。

我尝试了以下方法,但它没有给我想要的结果(显然都是小写):

String input = inputStringBuilder.toString();
input = input.replaceAll("[^äÄöÖüÜßa-zA-Z ]", "").toLowerCase();
String[] sentence = input.split(" ");

Map<String, Set<String>> anagrams = new HashMap<>();

for(int i = 0; i < sentence.length; i++){

        char[] charwords = sentence[i].toCharArray();

        Arrays.sort(charwords);

        String key = new String(charwords);

        Set<String> anagramSet = anagrams.get(key);
        if (anagramSet == null) {
          anagramSet = new HashSet<>();
          anagrams.put(key, anagramSet);
        }

   anagramSet.add(sentence[i]);

}

最佳答案

首先您需要移动toLowerCase()打电话。

input = input.replaceAll("[^äÄöÖüÜßa-zA-Z ]", ""); // <== Removed from here
String[] sentence = input.split(" ");

Map<String, Set<String>> anagrams = new HashMap<>();

for(int i = 0; i < sentence.length; i++){

        char[] charwords = sentence[i].toLowerCase().toCharArray(); // <== Added here

        Arrays.sort(charwords);

        String key = new String(charwords);

        Set<String> anagramSet = anagrams.get(key);
        if (anagramSet == null) {
          anagramSet = new HashSet<>();
          anagrams.put(key, anagramSet);
        }

   anagramSet.add(sentence[i]);

}

接下来,您需要删除 anagrams 中的条目不包含任何实际字谜的 map 。

问题代码中完全缺少此步骤,其中带有 Set 的映射条目大小为 1 的字符不是实际的字谜。

现在 Set包含带有原始大小写的单词,非字谜词,例如 "The""the"可能存在,并且也必须被消除,假设没有真正的字谜。如果存在真正的字谜词,则应保留各种大小写变体。

要检查这一点,请将所有单词添加到小写集合中,并消除该新集合的大小是否为 1,否则保留保留大小写的集合。

// code from above here
for (Iterator<Set<String>> iter = anagrams.values().iterator(); iter.hasNext(); ) {
    Set<String> words = iter.next();
    if (words.size() == 1) {
        iter.remove(); // Not anagram: Single spelling only
    } else {
        Set<String> lower = new HashSet<>();
        for (String word : words)
            lower.add(word.toLowerCase());
        if (lower.size() == 1) {
            iter.remove(); // Not anagram: Multiple case variants, but all same spelling
        }
    }
}

测试

Input:  This is a test of 'the' and 'The'
Result: {}

Input:  This is a test of 'the', 'The', and 'eth'
Result: {eht=[the, The, eth]}
<小时/>

如果您不想保留同一单词的所有大小写变体,则只需使集合不区分大小写,使用 new TreeSet<>(String.CASE_INSENSITIVE_ORDER) .

(代码精简,其中一些使用 Java 8 功能)

Map<String, Set<String>> anagrams = new HashMap<>();
for (String word : input.replaceAll("[^äÄöÖüÜßa-zA-Z ]", "").split(" ")) {
    char[] letters = word.toLowerCase().toCharArray();
    Arrays.sort(letters);
    String key = new String(letters);
    anagrams.computeIfAbsent(key, k -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER))
            .add(word);
}
anagrams.values().removeIf(words -> words.size() == 1);

测试

Input:  This is a test of 'the' and 'The'
Result: {}

Input:  This is a test of 'the', 'The', and 'eth'
Result: {eht=[eth, the]}

Input:  This is a test of 'The', 'the', and 'eth'
Result: {eht=[eth, The]}

关于java - 有没有办法在搜索字谜时保留单词的大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58535374/

相关文章:

Java websocket : How to receive PongMessage from websocket client and respond to this message

java - 帮我选择一个键/值存储

string - 字符串 2 的字谜是字符串 1 的子字符串

c - Anagrams - 在 C 中使用链接和探测进行散列

java - 在 Linux 上从 Jersey 返回 Json 抛出异常

java - 文件夹中的文件存储在_nXm_矩阵中,其中n给出行数,m给出列数

java - 存储、更改和保存大量类对象的最佳方式

javascript - 使用Java中的Map从字谜数组中查找唯一的单词

c++ - 在 C++ 中,您将如何对字符串进行排序以使字谜彼此接近?

c - 使用c中的链表检测变位词